I read the documentation for Process.StandardOutput, which has this quote:
A deadlock condition can result 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 wondering. What is the correct way to do this if I'm also afraid that StandardError could be filled in some scenarios?
Do I have to use a loop to alternate reading from standard output and error, to avoid either filling up, or is this simple code enough:
string error = proc.StandardError.ReadToEnd();
string output = proc.StandardOutput.ReadToEnd();
bool didFinish = proc.WaitForExit(60000);
Edited after some answers have been posted
So this is 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 stringbuilder content only if the process actually finished.
Is that the right approach then?