Methods prefixed with Begin typically run asynchronously. Is this what you want? Because it appears that the loop runs as fast as it can, calling more and more BeginOutputReadLine (because the call returns immediately, before it's done and the loop comes around for another iteration).
You could call ReadLine on the StandardOutput stream for a synchronous solution. Or:
while ( !p.HasExited ) {
/* ... - irrelevant */
if ( redirect && !busy ) {
try {
busy = true;
p.BeginOutputReadLine();
} catch { }
}
}
////In the method that gets called upon completion of BeginOutputReadLine
busy = false;
Keep this in mind though (from MSDN):
When asynchronous read operations start, the event handler is called each time the associated Process writes a line of text to its StandardOutput stream.
You can cancel an asynchronous read operation by calling CancelOutputRead. The read operation can be canceled by the caller or by the event handler. After canceling, you can call BeginOutputReadLine again to resume asynchronous read operations.
That leads me to believe you should only call this method once and it will keep notifying you through callback when a line gets written.