Cancels the asynchronous read operation on the redirected Process.StandardOutput stream of an application.
Process.BeginOutputReadLine starts an asynchronous read operation on the Process.StandardOutput stream. Process.CancelOutputRead ends the asynchronous read operation.
After canceling, you can resume asynchronous read operations by calling Process.BeginOutputReadLine again.
When you call Process.CancelOutputRead, all in-progress read operations for Process.StandardOutput are completed and then the event handler is disabled. All further redirected output to Process.StandardOutput is saved in a buffer. If you re-enable the event handler with a call to Process.BeginOutputReadLine, the saved output is sent to the event handler and asynchronous read operations resume. If you want to change the event handler before resuming asynchronous read operations, you must remove the existing event handler before adding the new event handler:
Example
// At this point the DataReceivedEventHandler(OutputHandler1) // has executed a CancelOutputRead. // Remove the prior event handler. process.OutputDataReceived -= new DataReceivedEventHandler(OutputHandler1); // Register a new event handler. process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler2); // Call the corresponding BeginOutputReadLine. process.BeginOutputReadLine();
You cannot mix asynchronous and synchronous read operations on the redirected Process.StandardOutput stream. Once the redirected stream of a System.Diagnostics.Process is opened in either asynchronous or synchronous mode, all further read operations on that stream must be in the same mode. If you cancel an asynchronous read operation on Process.StandardOutput and then need to read from the stream again, you must use Process.BeginOutputReadLine to resume asynchronous read operations. Do not follow Process.CancelOutputRead with a call to the synchronous read methods of Process.StandardOutput such as System.IO.StreamReader.Read, System.IO.StreamReader.ReadLine, or System.IO.StreamReader.ReadToEnd.