As discussed here ProcessStartInfo hanging on "WaitForExit"? Why? - calling p.WaitForExit() with a large output fills the OutputStream and causes a deadlock as the process and the output stream wait for each other.
My code for example:
Dim p = New Process()
Dim ReturnValue As Boolean = False
p.StartInfo = New ProcessStartInfo(LynxPath, "-dump -nolist -width 1000 " & HtmlBuffer)
p.StartInfo.WorkingDirectory = WorkingRoot
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.StartInfo.CreateNoWindow = True
p.Start()
ReturnValue = p.WaitForExit(5000)
When dealing with a large output from LYNX the thread hangs unless i use a timeout as above, and the output is trimmed when the output buffer gets full - meaning any output i do read isnt complete.
The c# solution posted in the question above seems to get around this by making use of the OutputDataReceived
event on the process class. My problem however is converting that c# code into vb.net 3.5, i ran it through the normal conversion routes and it spat out:
Using process As New Process()
process.StartInfo.FileName = filename
process.StartInfo.Arguments = arguments
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.RedirectStandardError = True
Dim output As New StringBuilder()
Dim [error] As New StringBuilder()
Using outputWaitHandle As New AutoResetEvent(False)
Using errorWaitHandle As New AutoResetEvent(False)
process.OutputDataReceived += Function(sender, e)
If e.Data Is Nothing Then
outputWaitHandle.[Set]()
Else
output.AppendLine(e.Data)
End If
End Function
process.ErrorDataReceived += Function(sender, e)
If e.Data Is Nothing Then
errorWaitHandle.[Set]()
Else
[error].AppendLine(e.Data)
End If
End Function
process.Start()
process.BeginOutputReadLine()
process.BeginErrorReadLine()
' Process completed. Check process.ExitCode here.
If process.WaitForExit(timeout) AndAlso outputWaitHandle.WaitOne(timeout) AndAlso errorWaitHandle.WaitOne(timeout) Then
' Timed out.
Else
End If
End Using
End Using
End Using
However Visual Studio 2008 flags the function declerations as invalid syntax (Inline methods such as this are in 4.5 onwards i think?), how would one make use of this example in 3.5 - cant quite get my head round it.
Edit: just found this link: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx - Trying to figure it out now