1

there is 2 textbox and 1 button

button1 = start cmd process
textbox1 = type command that you want to run it on cmd
textbox2 = live results of cmd process

in this case i replace textbox1 to "ping www.google.com" & "ping www.facebook.com"
the problem is.. result is not coming untill cmd is close
cmd is not automatic close.. i have to close it by myself

this is my code

Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Threading
Imports System.Windows
Public Class Form1

    Private Results As String
    Private Delegate Sub delUpdate()
    Private Finished As New delUpdate(AddressOf UpdateText)
    Dim myprocess As New Process
    Dim StartInfo As New System.Diagnostics.ProcessStartInfo

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        StartInfo.FileName = "cmd" 
        StartInfo.RedirectStandardInput = True
        StartInfo.RedirectStandardOutput = True
        StartInfo.UseShellExecute = False 
        StartInfo.CreateNoWindow = False
        myprocess.StartInfo = StartInfo
        myprocess.Start()

        Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
        CMDThread.Start()
        myprocess.StandardInput.WriteLine("ping www.google.com")
        myprocess.StandardInput.WriteLine("ping www.facebook.com")
    End Sub

    Private Sub CMDAutomate()
        On Error Resume Next
        While myprocess.StandardOutput.EndOfStream = False
            Results = myprocess.StandardOutput.ReadToEnd.ToString()
            Invoke(Finished)
        End While
    End Sub

    Private Sub UpdateText()
        TextBox2.AppendText(System.Environment.NewLine() & Results)
        TextBox2.ScrollToCaret()
    End Sub

End Class
DREAM
  • 426
  • 2
  • 14
  • 1
    [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). Read the notes there. Btw, all the BeginInvoke() calls are not needed when you set the `SynchronizingObject`. It was there for testing whether there's a difference (spoiler alert: there isn't). – Jimi Apr 18 '20 at 11:18

1 Answers1

1

That ReadToEnd method doesn't mean "read whatever is currently available". It means "read to the end of the stream". That means that you won't get anything back until there is an end to the stream. It's just like a call to Console.ReadLine doesn't return until there is a full line to read. If you want to get the output in chunks then you have to call a method that gets a chunk and do so repeatedly until there are no more chunks. The obvious option seems to be ReadLine, but it's up to you.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46