Process.OutputDataReceived Ereignis

Definition

Tritt jedes Mal auf, wenn eine Anwendung eine Zeile in ihren umgeleiteten StandardOutput-Stream schreibt.Occurs each time an application writes a line to its redirected StandardOutput stream.

public:
 event System::Diagnostics::DataReceivedEventHandler ^ OutputDataReceived;
public event System.Diagnostics.DataReceivedEventHandler? OutputDataReceived;
public event System.Diagnostics.DataReceivedEventHandler OutputDataReceived;
[System.ComponentModel.Browsable(true)]
public event System.Diagnostics.DataReceivedEventHandler OutputDataReceived;
member this.OutputDataReceived : System.Diagnostics.DataReceivedEventHandler 
[<System.ComponentModel.Browsable(true)>]
member this.OutputDataReceived : System.Diagnostics.DataReceivedEventHandler 
Public Custom Event OutputDataReceived As DataReceivedEventHandler 
Public Event OutputDataReceived As DataReceivedEventHandler 

Ereignistyp

DataReceivedEventHandler
Attribute

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie asynchrone Lesevorgänge für den umgeleiteten StandardOutput Stream des Befehls durchgeführt werden ipconfig .The following example illustrates how to perform asynchronous read operations on the redirected StandardOutput stream of the ipconfig command.

Im Beispiel wird ein Ereignis Delegat für den OutputHandler Ereignishandler erstellt und dem- OutputDataReceived Ereignis zugeordnet.The example creates an event delegate for the OutputHandler event handler and associates it with the OutputDataReceived event. Der Ereignishandler empfängt Textzeilen aus dem umgeleiteten StandardOutput Stream, formatiert den Text und speichert ihn in einer Ausgabe Zeichenfolge, die später im Konsolenfenster des Beispiels angezeigt wird.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();
}
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))
            {
                lineCount++;
                output.Append("\n[" + lineCount + "]: " + e.Data);
            }
        });

        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();
    }
}
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
        process.Start()

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

        Console.WriteLine(output)

        process.WaitForExit()
        process.Close()

        Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
        Console.ReadLine()
    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

Hinweise

Das- OutputDataReceived Ereignis gibt an, dass die zugeordnete Process eine Zeile geschrieben hat, die mit einem Zeilenumbruch Zeichen (Wagen Rücklauf (CR), Zeilenvorschub (LF) oder CR + LF) in den umgeleiteten Datenstrom beendet wird StandardOutput .The OutputDataReceived event indicates that the associated Process has written a line that's terminated with a newline (carriage return (CR), line feed (LF), or CR+LF) to its redirected StandardOutput stream.

Das-Ereignis wird bei asynchronen Lesevorgängen für aktiviert StandardOutput .The event is enabled during asynchronous read operations on StandardOutput. Um asynchrone Lesevorgänge zu starten, müssen Sie den Daten StandardOutput Strom einer umleiten Process , den Ereignishandler zum OutputDataReceived -Ereignis hinzufügen und den-Befehl aufzurufen BeginOutputReadLine .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. Danach signalisiert das OutputDataReceived Ereignis jedes Mal, wenn der Prozess eine Zeile in den umgeleiteten StandardOutput Stream schreibt, bis der Prozess beendet wird oder aufruft CancelOutputRead .Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.

Hinweis

Die Anwendung, die die asynchrone Ausgabe verarbeitet, sollte die-Methode aufzurufen WaitForExit , um sicherzustellen, dass der Ausgabepuffer geleert wurde.The application that is processing the asynchronous output should call the WaitForExit method to ensure that the output buffer has been flushed.

Gilt für:

Siehe auch