开发者

ResGen.exe stucks when redirect output

I try to redirect standard output from ResGen.exe. I use following code

ProcessStartInfo psi = new ProcessStartInfo( "resxGen.exe" );
psi.CreateNoWindow = true;
psi.Arguments = sb.ToString();
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
Process p = Process.Start( psi );
p.WaitForExit();
StreamReader sr = p.StandardOutput;
string message = p.StandardOutput.ReadToEnd();

It stuck on p.WaitForExit. When I turn off output stream redirection and do not read StandardOutput it works correctly.

W开发者_StackOverflowhat do I do wrong ?


You would need to wait for the process to end after reading the stream, otherwise you have a deadlock in your code. The problem is that your parent process is blocking waiting for the child process to finish, and the child process is waiting for the parent process to read the output, hence you have a deadlock.

Here is a good and detailed description of the problem.

Changing your code like this should avoid the deadlock:

StreamReader sr = p.StandardOutput;
string message = p.StandardOutput.ReadToEnd();
p.WaitForExit();


The bottom line would seem to be that the placement of p.WaitForExitis incorrect; this method call should be made only after reading what you want from the stream.

From MSDN:

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

Also note that your use of StreamReader sr = p.StandardOutput is redundant here since when the value ofmessage is set you access the stream using p.StandardOutput.ReadToEnd(); - note the p.StandardOutput as opposed to sr.ReadToEnd().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜