0

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?

user32882
  • 5,094
  • 5
  • 43
  • 82
  • You never reach the end of stream or you are getting an exception. ReadLine() is probably waiting for an end of line and is blocking. The standard output if it doesn't terminate with a return will just wait forever. – jdweng Jul 28 '23 at 14:11
  • [related](https://stackoverflow.com/questions/1145969/processinfo-and-redirectstandardoutput) – jeb Jul 28 '23 at 14:13
  • @jdweng There is no exception, when I inspect the value of `Proc.StandardOutput.EndOfStream` in my locals on VS Code it just says `Evaluation timed out`. Also if what you are describing was the case, shouldn't it also wait forever when I run the process *without* redirecting STDOUT? – user32882 Jul 28 '23 at 14:15
  • The last character of the stream does not produce the End Of Stream. You get the End of Stream when you read one character past the last character. Your code is reading the last character than waiting for a return before reading one character past the last character so you never get the End Of Stream. – jdweng Jul 28 '23 at 14:23
  • @jdweng sorry but what you are explaining isn't super helpful. – user32882 Jul 28 '23 at 14:24
  • The last character of the Standard Output is not a Return. So you are simply waiting for a Return at ReadLine() which you never get. – jdweng Jul 28 '23 at 14:33
  • @jdweng I don't think that's what's going on here... sorry – user32882 Jul 28 '23 at 14:35
  • From VS Debug window use Break All and check what line you are running. Standard Output does not add a Return at end of data. Seen issue a lot of times. – jdweng Jul 28 '23 at 14:39
  • What do you mean "check what line you are running"? I already said it hangs on the last line of STDOUT? And I'm using VS Code not VS – user32882 Jul 28 '23 at 14:41
  • The following may be of interest: https://stackoverflow.com/a/71846819/10024425 and https://stackoverflow.com/a/72818271/10024425 – Tu deschizi eu inchid Jul 28 '23 at 15:25

1 Answers1

0

You need to use OutputDataReceived / ErrorDataReceived event, along with BeginOutputReadLine() / BeginErrorReadLine() :

    Proc.OutputDataReceived += (_, p_Output)
        => {
            if (p_Output.Data is not null) {
                Console.WriteLine (p_Output.Data);
            }
        };
    Proc.Start();
    Proc.BeginOutputReadLine ();
    Proc.WaitForExit();