Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Diagnostics.Process not Exiting in Code

Tags:

c#

.net

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();
like image 323
NomadicDeveloper Avatar asked Feb 15 '12 17:02

NomadicDeveloper


Video Answer


2 Answers

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.

like image 175
penartur Avatar answered Oct 11 '22 08:10

penartur


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

like image 25
NomadicDeveloper Avatar answered Oct 11 '22 09:10

NomadicDeveloper