Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.StandardOutput.ReadToEnd();
This works if the application outputs very little amount of string and
exits quickly. But what if the application does a lengthy job and
outputs the progress and my C# application should be notified if the
job is progressed? Should I periodically invoke ReadToEnd() in a while
loop? Seems a waste of cpu cycles. Can't I just get notified when the
application outputs something instead? I tried to search the Internet
myself, but I couldn't find an answer.
Andrew Faust - 21 Oct 2007 23:55 GMT
Sure can. The Process class has an event called OutputDataReceived that is
reaised when something is written to the redirected StandardOutput stream.
http://msdn2.microsoft.com/en-us/library/system.diagnostics.process_members.aspx

Signature
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
> Process p = new Process();
> p.StartInfo.RedirectStandardOutput = true;
[quoted text clipped - 9 lines]
> application outputs something instead? I tried to search the Internet
> myself, but I couldn't find an answer.
Peter Duniho - 22 Oct 2007 00:22 GMT
> [...]
> p.StandardOutput.ReadToEnd();
[quoted text clipped - 3 lines]
> outputs the progress and my C# application should be notified if the
> job is progressed? [...]
As an alternative to using the OutputDataReceived event mentioned in the
other reply, you may also just use the async methods on the
StandardOutput stream. They will work just as the async methods for
Stream do with other streams, and provide nice asynchronous access to
the data being written to the stream.
You definitely would not want to call ReadToEnd() in a while loop. It
won't complete until the stream has actually ended (is closed), which
occurs when the process exits.
Pete