-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Process.BeginOutputReadLine can lead to lost stdout lines #18789
Comments
I've updated the test app to support non-Windows systems (by using The results are the same for Windows, Linux, and Mac OS X. |
@joelverhagen, for better or worse, what you're seeing is documented design... For Process.WaitForExit():
And for Process.WaitForExit(Int32):
You can see here that the code explicitly does not wait for the redirected output to complete if it was passed a non-infinite timeout:
That said, I don't know why it was designed this way. |
@joelverhagen Using WaitForExit without timeout after the other overload should unblock you, as pointed out in the docs. Closing this issue, please reopen if further assistance needed. |
When writing code that creates child processes and needs to capture stdout, I have found that lines of output go missing the following condition:
ProcessStartInfo.RedirectStandardOutput = true
Process.OutputDataReceived += CollectLines
(that is, using the async flow)Process.BeginOutputReadLine()
Process.WaitForExit(int.MaxValue)
Note that passing
-1
(infinite timespan) toWaitForExit
does not cause lines go missing (based on my own testing).I have created a little demo app to do this. It repro's on both
netcoreapp1.0
andnet45
: processredirect.zip. Just usedotnet run
to try it out.That being said, you can capture stdout by using
Process.StandardOutput
(which is aStreamReader
). However to consume this asynchronously and safely you need to use background threads/tasks. In other words:StandardOutput
StreamReader
StandardOutput
StreamReader
OutputDataReceived
eventOutputDataReceived
eventI think this a problem because it's pretty easy to step into this pitfall. Using the events to capture output is very convenient since you don't have to worry about making background threads or tasks. Also, the internet recommends this method:
I have only tried this on Windows, .NET Framework 4.5 and .NET Core (
netcoreapp1.0
). Also, all of this information is related to stdout, but I imagine stderr has the same problem. Mydotnet
info is:The text was updated successfully, but these errors were encountered: