1

Actually I want to display the messages shown in cmd prompt like, if i do:

Ping google.com -t

The following message will be displayed in the cmd prompt:

Reply from 74.125.235.17: bytes=32 time=133ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51

Ping statistics for 74.125.235.17:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 130ms, Maximum = 133ms, Average = 130ms

I want to display the exact information into the list box of my program instantly when it is displayed in the command prompt not after whole process is completed. How can i do so? Any help? I am using C#/vb.net .

As in the ping google.com -t, I want to display the each reply message instantly in the list box.

nightfire001
  • 759
  • 3
  • 20
  • 50

3 Answers3

2

For this you need to utilize async read on standardoutput... see also this...

Here you can find a solution for the described problem complete with source code... it even takes stderr into account...

Other interesting resources:

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • 2
    This link http://www.codeproject.com/KB/threads/ReadProcessStdoutStderr.aspx did the trick. Great, enjoy the bounty, Cheers! – Nick Binnet Sep 15 '11 at 12:56
1

Try this:

Private Results As String

'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text update.
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)

Private Sub UpdateText()
resultsTextBox.Text = Results
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub

Private Sub CMDAutomate()

Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo

'Starts the CMD Prompt
StartInfo.FileName = "cmd.exe"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True

'Required to redirect
StartInfo.UseShellExecute = False

'Disables the creation of a CMD Prompt outside application.
StartInfo.CreateNoWindow = True


myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput

'Runs the command you entered...
SW.WriteLine(TextBox1.Text)

'Exits CMD Prompt 
SW.WriteLine("exit")

'Displayes the results...
Results = SR.ReadToEnd
SW.Close()
SR.Close()

'Invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
Maysam
  • 7,246
  • 13
  • 68
  • 106
  • Mention that you need a tex box named resultsTextBox and a button named Button1 – Maysam Aug 07 '11 at 12:25
  • it's the output, there are some additional lines, you can remove them http://i.stack.imgur.com/RByCO.png – Maysam Aug 07 '11 at 17:49
  • Your above code work but it shows the result in the list box after all the work is finished. But I want to display the message instantly i.e. in the case of ping google.com -t; I want to display each reply message instantly in the list box. – nightfire001 Aug 09 '11 at 10:46
  • hm, maybe you have to deal with SR. – Maysam Aug 09 '11 at 12:07
0

Quick solution for you.

Community
  • 1
  • 1
OKEEngine
  • 888
  • 11
  • 28