Process::OutputDataReceived Event

 
System_CAPS_noteNote

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

Occurs each time an application writes a line to its redirected StandardOutput stream.

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

public:
[BrowsableAttribute(true)]
event DataReceivedEventHandler^ OutputDataReceived {
	void add(DataReceivedEventHandler^ value);
	void remove(DataReceivedEventHandler^ value);
}

The OutputDataReceived event indicates that the associated Process has written a line, terminating with a newline character, to its redirected StandardOutput stream.

The event is enabled during asynchronous read operations on StandardOutput. To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.

System_CAPS_noteNote

The application that is processing the asynchronous output should call the WaitForExit method to ensure that the output buffer has been flushed.

The following example illustrates how to perform asynchronous read operations on the redirected StandardOutput stream of the ipconfig command.

The example creates an event delegate for the OutputHandler event handler and associates it with the OutputDataReceived event. The event handler receives text lines from the redirected StandardOutput stream, formats the text, and saves it in an output string that’s later shown in the example’s console window.

using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;
using namespace System::Text;

ref class StandardAsyncOutputExample
{
private:
    static int lineCount = 0;
    static StringBuilder^ output = nullptr;

public:
    static void Run()
    {
        Process^ process = gcnew Process();
        process->StartInfo->FileName = "ipconfig.exe";
        process->StartInfo->UseShellExecute = false;
        process->StartInfo->RedirectStandardOutput = true;
        output = gcnew StringBuilder();
        process->OutputDataReceived += gcnew DataReceivedEventHandler(OutputHandler);
        process->Start();

        // Asynchronously read the standard output of the spawned process. 
        // This raises OutputDataReceived events for each line of output.
        process->BeginOutputReadLine();
        process->WaitForExit();

        // Write the redirected output to this application's window.
        Console::WriteLine(output);

        process->WaitForExit();
        process->Close();

        Console::WriteLine("\n\nPress any key to exit");
        Console::ReadLine();
    }

private:
    static void OutputHandler(Object^ sender, DataReceivedEventArgs^ e)
    {
        // Prepend line numbers to each line of the output.
        if (!String::IsNullOrEmpty(e->Data))
        {
            lineCount++;
            output->Append("\n[" + lineCount + "]: " + e->Data);
        }
    }
};

int main()
{
    StandardAsyncOutputExample::Run();
}

LinkDemand

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
Show: