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;
[System.ComponentModel.Browsable(true)]
public event System.Diagnostics.DataReceivedEventHandler OutputDataReceived;
member this.OutputDataReceived : System.Diagnostics.DataReceivedEventHandler 
Public Event OutputDataReceived As DataReceivedEventHandler 
Attribute

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie asynchrone Lesevorgänge für den umgeleiteten StandardOutput Stream des ipconfig-Befehls durchgeführt werden.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 Ereignis OutputDataReceived gibt an, dass die zugeordnete Process eine Zeile geschrieben hat, die mit einem Zeilen Umleitungs Zeichen endet, in den umgeleiteten StandardOutput Stream.The OutputDataReceived event indicates that the associated Process has written a line, terminating with a newline character, to its redirected StandardOutput stream.

Das Ereignis wird bei asynchronen Lesevorgängen auf StandardOutputaktiviert.The event is enabled during asynchronous read operations on StandardOutput. Um asynchrone Lesevorgänge zu starten, müssen Sie den StandardOutput Stream eines Processumleiten, den Ereignishandler dem OutputDataReceived-Ereignis hinzufügen und BeginOutputReadLineaufzurufen.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 Ereignis OutputDataReceived jedes Mal, wenn der Prozess eine Zeile in den umgeleiteten StandardOutput Datenstrom schreibt, bis der Prozess beendet wird oder CancelOutputReadaufruft.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 WaitForExit-Methode aufzurufen, 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.

Sicherheit

LinkDemand
für volle Vertrauenswürdigkeit für den unmittelbaren Aufrufer.for full trust for the immediate caller. Dieser Member kann von nur teilweise vertrauenswürdigem Code nicht verwendet werden.This member cannot be used by partially trusted code.

Gilt für:

Siehe auch