I read the documentation for Process.StandardOutput which has this quote in it:
A blocking condition may occur if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd, and the child process writes enough text to fill the redirected stream.
So I'm interested. What is the correct way to do this if I also fear that a StandardError might be populated in some scenarios?
Should I use a loop to alternate between reading from standard output and error to avoid either populating or simple enough code:
string error = proc.StandardError.ReadToEnd(); string output = proc.StandardOutput.ReadToEnd(); bool didFinish = proc.WaitForExit(60000);
Edited after posting multiple answers
So is this the right approach?
var output = new StringBuilder(); proc.OutputDataReceived += (s, e) => output.Append(e.Data); proc.BeginOutputReadLine(); string error = proc.StandardError.ReadToEnd(); bool didFinish = proc.WaitForExit(60000);
And then I use the contents of stringbuilder only if the process is complete.
Is this the right approach?
Lasse Vรฅgsรฆther Karlsen
source share