6

I have the following code which works well on another server. The problem is that the process never seems to make it to an Exited state. The exe being called creates a file as the last step and this file does get created but my code never seems to know that the process has completed. Also the exe being called runs in much less than 10 seconds when ran manually. My code looks like this:

                System.Diagnostics.Process proc = new System.Diagnostics.Process()    proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.FileName = exeConf.CMD;
                proc.StartInfo.Arguments = argString;
                proc.Start();
                proc.WaitForExit(10000);

                if(proc.HasExited)
                msgLine = proc.StandardError.ReadToEnd();
NomadicDeveloper
  • 857
  • 4
  • 17
  • 29

2 Answers2

13

See this MSDN article.

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. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.

penartur
  • 9,792
  • 5
  • 39
  • 50
5

It seems as if Process.StandardOutput.ReadToEnd() has to be called immediately after Process.Start() else it could create a deadlock.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
NomadicDeveloper
  • 857
  • 4
  • 17
  • 29
  • 2
    That does not make sense. As there might not be any output at all right after you have `System.Diagnostics.Process.Start()`ed. – Mike de Klerk Apr 05 '13 at 10:06