Process.OutputDataReceived and ErrorDataReceived come in a different order than console messages wou

  • Thread starter Thread starter DoB
  • Start date Start date
D

DoB

Hi,

I am posting this message again, because I got no answers for a long time.
Maybe this time I will be luckier ;-).

I am reading the process output and error streams, using:
...
p.OutputDataReceived += new
DataReceivedEventHandler(ConsoleEventHandler);
p.ErrorDataReceived += new
DataReceivedEventHandler(ConsoleEventHandler);
...

The problem is that I would like to receive these events in exactly the same
order in which they would be shown by the console if the streams were not
redirected (in other words: I would like to simulate the console).
Buth they come in a different order: generally, the error messages come
before the output messages and the overall result is not readable.

How to fix this? Is the StandardError buffered? If so, how to make it
unbuffered?

Regards,
DoB.
 
Thanks for your answer, I will try this.

I have another wild idea, but I am not sure whether it is possible.
I thought about redirecting the stdout and stderr to a pipe (named or
unnamed) and read the data from the pipe. I know how to read from a pipe,
however I am not sure if it is possible to redirect the streams to it.

The initial assumption (I have not said this clearly in the first post, so I
am doing it now) is that the process to be executed is to be treated "as
is". I cannot modify it, I can only execute it.

Regards,
DoB
 
Just to get you the preview of the problem. Suppose the java pogram that
writes 17 lines to the output and then crashes with a NullPointerException.

The oputput on the console looks like that:
--------------------------------------------------------------
line = 0
line = 1
line = 2
line = 3
line = 4
line = 5
line = 6
line = 7
line = 8
line = 9
line = 10
line = 11
line = 12
line = 13
line = 14
line = 15
line = 16
line = 17
Exception in thread "main" java.lang.NullPointerException
at rzygacz.metodaTrzecia(rzygacz.java:5)
at rzygacz.metodaDruga(rzygacz.java:9)
at rzygacz.metodaPierwsza(rzygacz.java:13)
at rzygacz.main(rzygacz.java:36)
--------------------------------------------------------------

Whereas when I try to simulate the console, the output looks as follows:
--------------------------------------------------------------
line = 0
Exception in thread "main" java.lang.NullPointerException
at rzygacz.metodaTrzecia(rzygacz.java:5)
at rzygacz.metodaDruga(rzygacz.java:9)
line = 1
line = 2
line = 3
at rzygacz.metodaPierwsza(rzygacz.java:13)
at rzygacz.main(rzygacz.java:36)
line = 4
line = 5
line = 6
line = 7
line = 8
line = 9
line = 10
line = 11
line = 12
line = 13
line = 14
line = 15
line = 16
line = 17
--------------------------------------------------------------

Now you might see that this is quite unreadable.
Using base streams does not help too much.

Does anyone know how the console works?

Regards,
DoB.
 
Back
Top