ResGen.exe stucks when redirecting output

I am trying to redirect standard output from a ResGen.exe file. I am using the 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();

He's stuck on p.WaitForExit. When I turn off output redirection and don't read StandardOutput, it works correctly.

What am I doing wrong?

+3
source share
2 answers

You will need to wait for the process to complete after reading the stream, otherwise you will have a dead end in your code. The problem is that your parent process blocks waiting for the child process to complete, and the child process waits for the parent process to read the result, so you have a dead end.

Here is a good and detailed description of the problem.

, , :

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

, p.WaitForExit ; , .

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

, StreamReader sr = p.StandardOutput , message p.StandardOutput.ReadToEnd(); - p.StandardOutput, sr.ReadToEnd().

+1

All Articles