NomadicDeveloper
NomadicDeveloper

Reputation: 889

System.Diagnostics.Process not Exiting in Code

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();

Upvotes: 6

Views: 4712

Answers (2)

NomadicDeveloper
NomadicDeveloper

Reputation: 889

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

Upvotes: 6

penartur
penartur

Reputation: 9912

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.

Upvotes: 13

Related Questions