System.Diagnostics.Process, RedirectStandardOutput process hanging

Posted at: 5/25/2006 at 11:19 AM by saravana
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";
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();


BTW, most of explanation is from MSDN, but it took me a while to figure out.
Tags:  Categories: .NET
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(12) | Comments RSS

Comments

Friday, July 13, 2007 2:44 PM
Torial
Thanks, that helped me w/ a problem I was having Smile
Wednesday, December 10, 2008 7:27 AM
ahmad U.A.E.
ahmad
Great explanation... Thanks a lot man... really helped out.. Smile
Tuesday, January 27, 2009 5:40 AM
Bradley Beach
umm... that sample was taken directly from MSDN Smile
Saturday, January 31, 2009 9:04 AM
saravana
Hi Bradley,
That's what I've mentioned in the last line right, I didn't deny the fact I found the solution in MSDN.


Monday, August 02, 2010 4:39 AM
lenen
Over de voor- en nadelen van het afsluiten van een lening zonder BKR-toetsing.
Thursday, August 05, 2010 1:43 AM
Seattle Limousines
This is such a wonderful useful resource that you'll be providing and you provide it away for free of charge
Saturday, August 07, 2010 3:51 PM
Sex Chat
I didn't see a link anywhere but do you have advertising? I have several blogs in the same niche and I'd like to add my banner somwhere on your site. This site seems to have a lot of discussion and visitors.
Saturday, August 07, 2010 4:18 PM
Nude Cam
Established you being a fave so I'll; be back again
Wednesday, August 18, 2010 5:31 PM
pdf ebook templates
This blog looks good. Keep up the good work!
Sunday, August 29, 2010 8:43 PM
cedar siding
Sunday, August 29, 2010 9:01 PM
tri city builders
Sunday, August 29, 2010 9:34 PM
richland siding

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading