June 7, 2009

Program started via system diagnostics process hangs

The other day I was reading the standard output of another process I had created. If the output happened to be somewhat longer then the application would hang. I found that really odd and started to blame the external application at first, but after consulting to google™ I was told what I was doing it wrong. The compiler could of had warned be about the issue but it didn’t and I was left with a hanging application, which sucked. In short you have to make sure your StandardError stream wont pile up its precious buffer of 4096 bytes.

[…] you have a more general problem, which is the possibility that the standard output
will wind up blocked because you haven’t been reading from standard error.

In most cases, this may not be a problem, assuming the output to
standard error is brief. But it’s a basic issue with redirection of the
Process class standard i/o streams: if you redirect more than one, you
need to make sure you are using some form of asynchronous access to send
or receive data from all of them as appropriate. Otherwise, the console
application winds up blocked trying to read or write to one of the
streams, causing a form of deadlock.

The documentation for the Process class has a good, detailed explanation
of this. It’s helpful to read that if you intend to redirect the streams.