In one of our internal web app’s developement, we were executing an console applications with certain arguments synchronously and were trying to display the results in the front-end.
When we activate the process I can see the process getting kicked-off in the task manager and it never exits, after a time period (IIS timeout setting), the web app times out.
At the end we figured out, the reason is due to an dead lock condition between the parent process (w3wp) and the child process(console app).
Synchronous read operations introduce a dependency between the caller reading from the StandardOutput stream and the child process writing to that stream. These dependencies can result in deadlock conditions. When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits on the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits on the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait on each other to complete an operation, and neither can proceed.
Solution is really simple, look at the code snippet below.
// 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”;
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
BTW, most of explanation is from MSDN, but it took me a while to figure out.