I have a process which behaves differently when I redirect standard output to when I do not. If I launch the process as follows:
using (Process Proc = new Process())
{
Proc.StartInfo.FileName = "Runner.exe";
Proc.StartInfo.Arguments = $"...";
Proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.CreateNoWindow = false;
Proc.StartInfo.RedirectStandardOutput = false;
Proc.Start();
Proc.WaitForExit();
int exitCode = Proc.ExitCode;
Console.WriteLine($"Done with exit code {exitCode}");
}
I get the standard output of the process in my terminal followed by Done with exit code 0
which implies the process is fine.
However if I launch the process as follows:
using (Process Proc = new Process())
{
Proc.StartInfo.FileName = "Runner.exe";
Proc.StartInfo.Arguments = $"...";
Proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.CreateNoWindow = false;
Proc.StartInfo.RedirectStandardOutput = true;
Proc.Start();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
Console.WriteLine(line);
}
Proc.WaitForExit();
int exitCode = Proc.ExitCode;
Console.WriteLine($"Done with exit code {exitCode}");
}
The process lags and never runs to completion. Upon debugging, it became apparent that the program gets stuck on process.StandardOutput.EndOfStream
in the last iteration of the while
loop and I can't quite figure out why.
Any ideas or pointers as to what might be going on here?