正如這裏所討論的ProcessStartInfo hanging on "WaitForExit"? Why? - 調用具有大輸出的p.WaitForExit()會填充OutputStream並導致死鎖,因爲進程和輸出流等待每個其他。使用RedirectStandardOutput處理大量的p.WaitForExit()輸出= True
我的示例代碼:
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)
當與LYNX一個輸出大危在旦夕,除非我使用超時如上處理,當輸出緩衝區滿輸出修剪 - 這意味着任何輸出我讀不完整。
上述問題中發佈的c#解決方案似乎通過在進程類中使用OutputDataReceived
事件來解決此問題。我的問題是不過是將C#代碼轉換成vb.net 3.5,我跑它通過正常的轉換路線和它吐出來:
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
但是Visual Studio 2008和的標誌功能declerations爲無效的語法(聯方法如這是在4.5以後,我認爲?),如何在3.5中使用這個例子 - 不能讓我頭腦發熱。
編輯:剛剛發現這個鏈接:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx - 試圖弄清楚現在
您可能會感興趣[此帖](HTTP://www.codeducky。 org/process-handling-net),它解釋瞭如何使用異步任務來處理進程流,而不是使用較爲笨拙的數據接受的事件 – ChaseMedallion