DeadLock Issues in Process.StandardOutput.ReadToEnd()?

In short this is what may happen: Application A (your code above) starts child process B and redirects standard output. Then A waits for the B process to exit. While A waits for B to exit, B produces output into the output stream (which A has redirected).

This stream has a limited buffer size. If the buffer becomes full, it needs to be emptied in order to B to be able to continue writing into it. Since A is not reading until B has exited, you can end up in a situation where B will wait for the output buffer to be emptied, while A will wait for B to exit.

Both are waiting for each other to take action, and you have a deadlock. You can try the following code to demonstrate the problem: ProcessStartInfo psi = new ProcessStartInfo(); psi. FileName = "cmd"; psi.

Arguments = @"/c dir C:\windows /s"; psi. RedirectStandardOutput = true; psi. UseShellExecute = false; Process p = Process.

Start(psi); p.WaitForExit(); string output = p.StandardOutput.ReadToEnd(); This will (moste likely) produce the situation where the output stream is full so that the child process (in this case "cmd") will wait for it to be cleared, while the code above will wait for cmd to finish.

Thanks for your lucid explanation. – Graviton Sep 25 '09 at 8:23 So ... how can I run a process such as sqlcmd. Exe with some arguments for not much longer than 5 minutes and read as much of the stdout and stderr as I can capture?

I could not find a clean and simple way to do this. – Hamish Grubijan Apr 5 at 19:46.

The parent process is the one calling p.Start(). I guess this is your application (the caller). The child process is p or in other words, the callee.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions