Reading from redirected Process Error stream

  • Thread starter Thread starter PIEBALD
  • Start date Start date
P

PIEBALD

I have a Process, I redirect the input, output, and error streams (and set
UseShellExecute = false ).

I can write with process.StandardInput.BaseStream.Write

I can read with process.StandardOutput.BaseStream.Read and receive 0 when
there is no data

But when I try to read with process.StandardError.BaseStream.Read (or
ReadByte) it blocks (or hangs?)
I expect to receive 0 (or -1) when there are no bytes to read.

Why do the two streams behave differently?

(Don't tell me to use BeginRead; that wouldn't answer my question.)
 
PIEBALD said:
I have a Process, I redirect the input, output, and error streams (and set
UseShellExecute = false ).

I can write with process.StandardInput.BaseStream.Write

I can read with process.StandardOutput.BaseStream.Read and receive 0 when
there is no data

That's not correct. Stream.Read() returns 0 only when the end of the
stream is reached. It simply blocks if it's just that no data is
currently available yet.
But when I try to read with process.StandardError.BaseStream.Read (or
ReadByte) it blocks (or hangs?)
I expect to receive 0 (or -1) when there are no bytes to read.

Why do the two streams behave differently?

They don't. Either reader works the same, and will block if you attempt
to read from it when there is no data available at the moment. Likewise
the base stream will block.

The readers return null and the stream returns 0 when the
_end-of-stream_ is reached, which does not happen until the process has
exited.

Note that if you do not handle _simultaneous_ reading of both
streams/readers, then if you are only reading one and the other's buffer
gets filled, then everything will come to a halt. If you redirect both,
you must make sure to be able to read both at the same time.

Pete
 
Back
Top