Process.CancelOutputRead Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

member CancelOutputRead : unit -> unit

Exception Condition

The StandardOutput stream is not enabled for asynchronous read operations.

BeginOutputReadLine starts an asynchronous read operation on the StandardOutput stream. CancelOutputRead ends the asynchronous read operation.

After canceling, you can resume asynchronous read operations by calling BeginOutputReadLine again.

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

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

You cannot mix asynchronous and synchronous read operations on the redirected StandardOutput 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 StandardOutput and then need to read from the stream again, you must use BeginOutputReadLine to resume asynchronous read operations. Do not follow CancelOutputRead with a call to the synchronous read methods of StandardOutput 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