StandardOutput.EndOfStream Hangs

。_饼干妹妹 提交于 2019-11-29 15:03:01

You cannot use EndOfStream reliably here. The StreamReader.EndOfStream property will call StandardOutput.Read() if it doesn't have any characters buffered. That Read() call will block if the process isn't sending anything to its output pipe and doesn't close it. Which is pretty much guaranteed to happen since it will be waiting for input. EndOfStream won't return true until the process has closed its end of the output pipe and the StreamReader has consumed all its buffered characters. At program termination.

Using BeginOutputReadLine() might be a better way to detect the "marker" line. Beware that the callback happens on another thread. Also note that it shouldn't be necessary to wait for the process to send the marker, anything you write will be buffered until the process is ready to read it. Beware that the buffers are smallish, deadlock is possible.

There are numerous paths for which you can create a deadlock when interacting with a process class. Microsoft describes them on the MSDN site here. Here is how I call it. Notice the handling of ErrorDataReceived and OutputDataReceived and the calls to BeginErrorReadLine and BeginOutputReadLine. This eliminates deadlock scenarios by having the parent process read the streams asynchronously. NOTE: RunProcessResponse is my own little wrapper data transfer object.

Public Function RunProcess(ByVal executableFileName As String, ByVal arguments As String, ByVal workingDirectory As System.String) As RunProcessResponse
    Dim process As System.Diagnostics.Process = Nothing
    Dim response As RunProcessResponse

    Try
        process = New System.Diagnostics.Process()
        Dim psInfo As New System.Diagnostics.ProcessStartInfo()
        Dim errorString As System.String = String.Empty
        Dim outputString As System.String = String.Empty


        If Not System.String.IsNullOrEmpty(workingDirectory) Then
            psInfo.WorkingDirectory = workingDirectory
        End If

        psInfo.FileName = executableFileName
        psInfo.Arguments = arguments
        psInfo.WindowStyle = ProcessWindowStyle.Hidden
        psInfo.CreateNoWindow = True
        psInfo.RedirectStandardError = True
        psInfo.RedirectStandardOutput = True
        psInfo.UseShellExecute = False

        AddHandler process.ErrorDataReceived, Sub(sender As Object, args As DataReceivedEventArgs)
                                                  If args.Data IsNot Nothing Then
                                                      errorString &= args.Data & vbCrLf
                                                  End If
                                              End Sub
        AddHandler process.OutputDataReceived, Sub(sender As Object, args As DataReceivedEventArgs)
                                                   If args.Data IsNot Nothing Then
                                                       outputString &= args.Data & vbCrLf
                                                   End If
                                               End Sub

        process.StartInfo = psInfo
        process.Start()

        process.BeginErrorReadLine()
        process.BeginOutputReadLine()
        process.WaitForExit()

        response = New RunProcessResponse(errorString, outputString, process.ExitCode)

        Return response
    Finally
        If process IsNot Nothing Then
            process.Dispose()
        End If
    End Try
End Function

Did you wait for the process to finish before reading from it's standard output:

mProcess.WaitForExit();

If you know that there is no more standard input after your:

while (!mProcess.StandardOutput.EndOfStream) 

loop, you can close standard input with:

mProcess.StandardInput.Close();

To signal that there is no more input. As long as standard input is open, there is the potential for more input, thus more output; therefore, standard output will never reach EndOfStream.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!