Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
OutputDataReceived Event
Collapse the table of content
Expand the table of content

Process.OutputDataReceived Event

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

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

public event DataReceivedEventHandler OutputDataReceived

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.


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.

Imports System
Imports System.IO
Imports System.Diagnostics
Imports System.Text

Module Module1
    Dim lineCount As Integer = 0
    Dim output As StringBuilder = New StringBuilder()

    Sub Main()
        Dim process As New Process()
        process.StartInfo.FileName = "ipconfig.exe"
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardOutput = True 
        AddHandler process.OutputDataReceived, AddressOf OutputHandler

        ' Asynchronously read the standard output of the spawned process.  
        ' This raises OutputDataReceived events for each line of output.



        Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
    End Sub 

    Sub OutputHandler(sender As Object, e As DataReceivedEventArgs)
        If Not String.IsNullOrEmpty(e.Data) Then
            lineCount += 1

            ' Add the text to the collected output.
            output.Append(Environment.NewLine + "[" + lineCount.ToString() + "]: " + e.Data)
        End If 
    End Sub 
End Module
using System;
using System.IO;
using System.Diagnostics;
using System.Text;

class StandardAsyncOutputExample
    private static int lineCount = 0;
    private static StringBuilder output = new StringBuilder();

    public static void Main()
        Process process = new Process();
        process.StartInfo.FileName = "ipconfig.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
            // Prepend line numbers to each line of the output. 
            if (!String.IsNullOrEmpty(e.Data))
                output.Append("\n[" + lineCount + "]: " + e.Data);


        // Asynchronously read the standard output of the spawned process.  
        // This raises OutputDataReceived events for each line of output.

        // Write the redirected output to this application's window.


        Console.WriteLine("\n\nPress any key to exit.");
private static void SortOutputHandler(object sendingProcess, 
    DataReceivedEventArgs outLine)
    // Collect the sort command output. 
    if (!String.IsNullOrEmpty(outLine.Data))

        // Add the text to the collected output.
        sortOutput.Append(Environment.NewLine + 
            "[" + numOutputLines.ToString() + "] - " + outLine.Data);
   static void SortOutputHandler( Object^ /*sendingProcess*/,
      DataReceivedEventArgs^ outLine )
      // Collect the sort command output. 
      if (  !String::IsNullOrEmpty( outLine->Data ) )

         // Add the text to the collected output.
         sortOutput->AppendFormat( "\n[{0}] {1}",
            numOutputLines.ToString(), outLine->Data );

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • LinkDemand 

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

© 2015 Microsoft