Process.Start() redirecting output

S

sgt_pinky

Hi, I had the following code which works fine:

Public Function RunConsole(ByVal txtBox As
System.Windows.Forms.TextBox)
Dim p As New Process
p.StartInfo.FileName = "ping.exe"
p.StartInfo.Arguments = "10.0.0.69"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.Start()
With txtBox
.Text = .Text & p.StandardOutpur.ReadToEnd
.SelectionStart = Len(.Text)
.ScrollToCaret()
End With
p.WaitForExit()
End Function


Only problem is, its fully synchronous, and for the batch file I want
to run, i need to see the output throught the running process, not
just all the output when the process is finished.

My latest attempt at this looks like:

[code:1:894e449361]
Public Function RunConsole(ByVal txtBox As
System.Windows.Forms.TextBox)

Dim p As New Process
Dim bAvail, bRead As Integer
Dim bString As String

p.StartInfo.FileName = "ping.exe"
p.StartInfo.Arguments = "10.0.0.69"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.Start()

Do While p.HasExited = False
bAvail = p.StandardOutput.Peek
If bAvail <> -1 Then
bString = New String(Chr(0), bAvail)
p.StandardOutput.ReadBlock(bString, bAvail,
bRead)
Debug.WriteLine("bAvail: " &
bAvail)
Debug.WriteLine("bString: " &
bString)
Debug.WriteLine("bRead: " &
bRead)
If bRead = 0 Then GoTo not_this_time
With txtBox
.Text = .Text & bString
.SelectionStart = Len(.Text)
.ScrollToCaret()
End With
End If
not_this_time:
Loop

p.WaitForExit()

End Function
[/code:1:894e449361]

Any help much appreciated! Cheers. :wink:
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed)-spam.invalid (sgt_pinky) scripsit:
Hi, I had the following code which works fine:

Public Function RunConsole(ByVal txtBox As
System.Windows.Forms.TextBox)
Dim p As New Process
p.StartInfo.FileName = "ping.exe"
p.StartInfo.Arguments = "10.0.0.69"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.Start()
With txtBox
.Text = .Text & p.StandardOutpur.ReadToEnd
.SelectionStart = Len(.Text)
.ScrollToCaret()
End With
p.WaitForExit()
End Function


Only problem is, its fully synchronous, and for the batch file I want
to run, i need to see the output throught the running process, not
just all the output when the process is finished.

<http://dotnet.mvps.org/dotnet/samples/miscsamples/downloads/RedirectConsole.zip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top