lar3ry's Profile User Rating: *****

Reputation: 312 Architect
Group:
Contributors
Active Posts:
1,297 (1.19 per day)
Joined:
12-September 12
Profile Views:
16,437
Last Active:
User is offline Jul 26 2015 11:40 AM
Currently:
Offline

Previous Fields

Country:
CA
OS Preference:
Who Cares
Favorite Browser:
FireFox
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Ford
Dream Kudos:
25

Latest Visitors

Icon   lar3ry has not set their status

Posts I've Made

  1. In Topic: Can't update a TextBox from a handler

    Posted 25 May 2015

    View Postmaceysoftware, on 25 May 2015 - 01:48 AM, said:

    Would you care to share your answer, for anyone else who find this thread, nothing in your code suggest that its done on a separate thread, I also didn't require an invoke when writing it.

    scratch that before I was just using debug.print not actual controls I see how it works now


    Actually, yes. I did not share my answer because I needed to remove a few things from it (password generation and sending) before posting, and I really had to get to bed.

    There is one thing in the code that is actually unnecessary in this particular application. Because I know already that the Invoke is required, I could have just used it. The SetText Sub is a little more general, and can be used from the main thread or a separate thread.

    The only thing I don't like about this solution is that the incoming data is not available until a CR or LF (or both) comes in, which means that I can't trigger on the prompt in the Linux device, as it obviously does not have a CR or LF. The workaround I used is to send the command line with StandardInput.WriteLine(), but add an extra LF. This gives me two prompts returnig, but I can live with that. If anyone knows a way to collect the incoming data as it arrives, I welcome the info.

    Anyway, here's the code...

    
    Imports System
    Imports System.Text
    Imports System.IO
    Imports System.Diagnostics
    Imports System.Threading
    Imports System.ComponentModel
    Imports Microsoft.VisualBasic
    
    Public Class Form1
    
        Private MyProcess As New Process
        Delegate Sub SetTextCallback(newString As String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            With MyProcess.StartInfo
                .FileName = "CMD.exe"
                .UseShellExecute = False
                .CreateNoWindow = True
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
            End With
    
            AddHandler MyProcess.OutputDataReceived, _
                          AddressOf OutputHandler
    
            MyProcess.Start()
    
            MyProcess.BeginErrorReadLine()
    
            MyProcess.BeginOutputReadLine()
    
        End Sub
    
        Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
            MyProcess.StandardInput.WriteLine("plink -ssh -v -pw " & password & " root@172.16.1.95" & vbLf)
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            MyProcess.StandardInput.WriteLine("exit" & vbLf)
        End Sub
    
        Private Sub OutputHandler(sendingProcess As Object, _
            out_Line As DataReceivedEventArgs)
    
            ' Collect the command output.
            If Not String.IsNullOrEmpty(out_Line.Data) Then
                Debug.Print(out_Line.Data)
                SetText(out_Line.Data)
            End If
        End Sub
    
        Private Sub SetText(ByVal newString As String)
            ' Calling from another thread? -> Use delegate
    
            If Me.TextBox2.InvokeRequired Then
                Dim d As New SetTextCallback(AddressOf SetText)
                ' Execute delegate in the UI thread, pass args as an array
                Me.Invoke(d, New Object() {newString})
    
    	Else ' Same thread, assign string to the textbox
                Me.TextBox2.Text &= newString & vbCrLf
            End If
        End Sub
    
    End Class
    
    
  2. In Topic: Can't update a TextBox from a handler

    Posted 24 May 2015

    View Postlar3ry, on 24 May 2015 - 09:39 PM, said:

    I know (or at least I think I know) that I have to use Invoke() to call a delegate to get data back to the main thread, either in a TextBox or a String. I just can't figure out how to do that.


    Got it! Finally found an example that allowed me to figure out what each part of it was doing.
  3. In Topic: Can't update a TextBox from a handler

    Posted 24 May 2015

    View Postmaceysoftware, on 24 May 2015 - 04:42 PM, said:

    Hey there,

    The issue isn't that the method is happening on a different thread its because your variable and method are shared.

    I have just tested this code in a test project with removing the shared key words (which you don't need) and the text is successfully re-directed.


    Thanks! That got me actually having data returned.

    Unfortunately, it still leaves me with my original problem I had in a previous iteration of the code. I get a run-time error:

    Cross-thread operation not valid: Control 'TextBox2' accessed from a thread other than the thread it was created on

    I know (or at least I think I know) that I have to use Invoke() to call a delegate to get data back to the main thread, either in a TextBox or a String. I just can't figure out how to do that.
  4. In Topic: How can I check elapsed time?

    Posted 3 Dec 2013

    Another, easy way is to use the StopWatch class.

    Globally,
    Public sw As New Stopwatch

    then later,

    sw.Start
    sw.Restart
    sw.Elapsed
    sw.Elapsed.ToString
  5. In Topic: Fasting Way to Execute Code...

    Posted 11 Nov 2013

    View Postkyle_denney, on 11 November 2013 - 01:49 PM, said:

    Any ideas on increasing the speed of the code I displayed? Thanks!

    As I said, you don't need to know what's in the file to shred it. You can write to the bytes without reading them, farting around with shifting bits around, and so on. That will increase your speed a LOT.

My Information

Member Title:
Coding Geezer
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Years Programming:
45
Programming Languages:
Various Assemblers, Fortran, Basic, Icon, Logo, PostScript, Rexx, COMAL, Pilot, C, C++, VB, VB.NET

Contact Information

E-mail:
Private

Comments

Page 1 of 1
  1. Photo

    C.Andrews Icon

    20 Mar 2013 - 11:31
    LOL. You know why. /hat
  2. Photo

    raghav.naganathan Icon

    19 Nov 2012 - 02:50
    Dude...you sure are a great asset in the VB forums :)
Page 1 of 1