Process.CancelErrorRead Method ()


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

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

member CancelErrorRead : unit -> unit

Exception Condition

The StandardError stream is not enabled for asynchronous read operations.

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

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

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

// 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.

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

The following example starts the nmake command with user supplied arguments. The error and output streams are read asynchronously; the collected text lines are displayed to the console as well as written to a log file. If the command output exceeds a specified number of lines, the asynchronous read operations are canceled.

No code example is currently available or this language may not be supported.


for full trust for the immediate caller. This member cannot be used by partially trusted code.

.NET Framework
Available since 2.0
Return to top