System.Diagnostics.Process.CancelErrorRead Method

Cancels the asynchronous read operation on the redirected Process.StandardError stream of an application.

Syntax

[System.Runtime.InteropServices.ComVisible(false)]
public void CancelErrorRead ()

Remarks

Process.BeginErrorReadLine starts an asynchronous read operation on the Process.StandardError stream. Process.CancelErrorRead ends the asynchronous read operation.

After canceling, you can resume the asynchronous read operation by calling Process.BeginErrorReadLine again.

When you call Process.CancelErrorRead, all in-progress read operations for Process.StandardError are completed and then the event handler is disabled. All further redirected output to Process.StandardError is saved in a buffer. If you re-enable the event handler with a call to Process.BeginErrorReadLine, 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(ErrorHandler1) 
    // has executed a CancelErrorRead.

    // Remove the prior event handler.
    process.ErrorDataReceived -= 
        new DataReceivedEventHandler(ErrorHandler1);

    // Register a new event handler.
    process.ErrorDataReceived += 
        new DataReceivedEventHandler(ErrorHandler2);

    // Call the corresponding BeginErrorReadLine.
    process.BeginErrorReadLine();
Note:

You cannot mix asynchronous and synchronous read operations on the redirected Process.StandardError 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.StandardError and then need to read from the stream again, you must use Process.BeginErrorReadLine to resume asynchronous read operations. Do not follow Process.CancelErrorRead with a call to the synchronous read methods of Process.StandardError such as System.IO.StreamReader.Read, System.IO.StreamReader.ReadLine, or System.IO.StreamReader.ReadToEnd.

Requirements

Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0