How to Access Page Controls Inside a Static Web Method

How to asynchronously read the standard output stream and standard error stream at once

I found the answer:

The output streams are buffered. There is no way to get the true
sequential order of the items inserted into the streams. In fact it
makes little sense as both streams can be written too at the same
time. They are independent of each other. Therefore the best you can
do is get the sequential output from each one as they arrive.

Generally this is not an issue though as almost all console apps use
standard output for both output and error messages. The error stream
is used by some apps but the messages are generally duplicates of the
errors generated in the output stream.

Source: http://social.msdn.microsoft.com/Forums/uk/csharpgeneral/thread/192b6df7-9437-42cf-81c1-c125021735ba

C# Capturing the output of a process asynchronous is always null

Reading the documentation of OutputDataReceived, you must call testProcess.BeginOutputReadLine().

You also might have to call testProcess.Start() after you set the OutputDataReceived event, as the example does.

How can I reliably read the full output of a process in c# when providing a max wait time to WaitForExit()?

There is an awkward implementation detail that is at play here.

Calling

p.WaitForExit();

and

p.WaitForExit(10000);

do slightly different things when the actual native processhandle gets signaled.

Internally p.WaitForExit(); calls p.WaitForExit(-1);. The -1 is significant here. Let's see what we have (code is simplified/ paraphrased to show the essence):

public bool WaitForExit(int milliseconds)
{
// init stuff removed
bool flag;
try
{
flag = processWaitHandle.WaitOne(milliseconds, false);
}
finally
{
// here we see our -1 return
if (this.output != null && milliseconds == -1)
{
this.output.WaitUtilEOF();
}
}
return flag;
}

In the above snippet you see this.output.WaitUtilEOF(); and that calls into an internal AsyncStreamReader that employs a queue. The call to WaitUtilEOF(); basically waits on the stream for the EOF event to be raised.

There is no other way that I could find to force the Process class to make a call to wait for those EOF events. The only option is to call WaitForExit() without a parameter. There is however no penalty in calling WaitForExit(); after a call to WaitForExit(10000) returned.

So if your timeout on the first WaitForExit(10000) was reached but you're sure you rather wait a bit longer for the AsyncStreamReader to hand you all data it has, call WaitForExit() without a parameter to have both AsyncStreamReaders empty their queue and then return control to you. This does mean that if your process didn't end you're now stuck in a wait that won't ever resolve itself unless you kill the child process by yourself.



Related Topics



Leave a reply



Submit