J
John Brock
I have written a short VB.NET function (see below) which captures
StandardOutput from a particular Windows program (myProgram.exe).
The problem is this: if myProgram.exe returns more than a single
line of text then WaitForExit() always times out! The function
works fine if myProgram.exe return only one line of text. It also
works fine with multi-line output -- returning a String with embedded
newlines -- provided I remove the test on WaitForExit(). Note that
if I execute myProgram.exe from the command line it returns quickly,
no matter how many lines of text I have asked for.
My understanding of WaitForExit() is that it returns True when the
process that it is waiting on exits, or False if the process has
not exited after a specified number of milliseconds.
Clearly I must have misunderstood something, but I see nothing in
the online documentation to suggest why it would matter whether
the the output from the program contains one line or many. Can
anyone tell me what I am doing wrong?
Public Function myFunction(ByVal myOptions As String) As String
Dim psInfo As New Diagnostics.ProcessStartInfo
psInfo.FileName = "myProgram.exe"
psInfo.Arguments = myOptions
psInfo.CreateNoWindow = True
psInfo.UseShellExecute = False
psInfo.RedirectStandardOutput = True
Dim newProc As Diagnostics.Process = Diagnostics.Process.Start(psInfo)
Dim myOutput As String = ""
If newProc.WaitForExit(6000) Then myOutput = newProc.StandardOutput.ReadToEnd
newProc.StandardOutput.Close()
newProc.Close()
newProc.Dispose()
Return myOutput
End Function
StandardOutput from a particular Windows program (myProgram.exe).
The problem is this: if myProgram.exe returns more than a single
line of text then WaitForExit() always times out! The function
works fine if myProgram.exe return only one line of text. It also
works fine with multi-line output -- returning a String with embedded
newlines -- provided I remove the test on WaitForExit(). Note that
if I execute myProgram.exe from the command line it returns quickly,
no matter how many lines of text I have asked for.
My understanding of WaitForExit() is that it returns True when the
process that it is waiting on exits, or False if the process has
not exited after a specified number of milliseconds.
Clearly I must have misunderstood something, but I see nothing in
the online documentation to suggest why it would matter whether
the the output from the program contains one line or many. Can
anyone tell me what I am doing wrong?
Public Function myFunction(ByVal myOptions As String) As String
Dim psInfo As New Diagnostics.ProcessStartInfo
psInfo.FileName = "myProgram.exe"
psInfo.Arguments = myOptions
psInfo.CreateNoWindow = True
psInfo.UseShellExecute = False
psInfo.RedirectStandardOutput = True
Dim newProc As Diagnostics.Process = Diagnostics.Process.Start(psInfo)
Dim myOutput As String = ""
If newProc.WaitForExit(6000) Then myOutput = newProc.StandardOutput.ReadToEnd
newProc.StandardOutput.Close()
newProc.Close()
newProc.Dispose()
Return myOutput
End Function