Process.OutputDataReceived Zdarzenie

Definicja

Występuje za każdym razem, gdy aplikacja zapisuje wiersz w przekierowanym StandardOutput strumieniu.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 

Typ zdarzenia

DataReceivedEventHandler
Atrybuty

Przykłady

Poniższy przykład ilustruje sposób wykonywania asynchronicznych operacji odczytu w przekierowanym StandardOutput strumieniu ipconfig polecenia.The following example illustrates how to perform asynchronous read operations on the redirected StandardOutput stream of the ipconfig command.

Przykład tworzy delegata zdarzenia dla OutputHandler programu obsługi zdarzeń i kojarzy go ze OutputDataReceived zdarzeniem.The example creates an event delegate for the OutputHandler event handler and associates it with the OutputDataReceived event. Program obsługi zdarzeń odbiera wiersze tekstu z przekierowanego StandardOutput strumienia, formatuje tekst i zapisuje je w ciągu wyjściowym, który jest później wyświetlany w oknie konsoli przykładu.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

Uwagi

OutputDataReceivedZdarzenie wskazuje, że skojarzony Process zapisał wiersz, który jest zakończony znakiem nowego wiersza (znak powrotu karetki (CR), wysuw wiersza (LF) lub CR + LF) do jego przekierowanego StandardOutput strumienia.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.

Zdarzenie jest włączane podczas asynchronicznych operacji odczytu w StandardOutput .The event is enabled during asynchronous read operations on StandardOutput. Aby rozpocząć asynchroniczne operacje odczytu, należy przekierować StandardOutput strumień obiektu Process , dodać obsługę zdarzenia do OutputDataReceived zdarzenia i wywołać metodę 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. Następnie OutputDataReceived Zdarzenie sygnalizuje każdorazowe zapisanie wiersza w przekierowanym StandardOutput strumieniu do momentu zakończenia procesu lub wywołania 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.

Uwaga

Aplikacja, która przetwarza asynchroniczne dane wyjściowe, powinna wywołać WaitForExit metodę, aby upewnić się, że bufor wyjściowy został opróżniony.The application that is processing the asynchronous output should call the WaitForExit method to ensure that the output buffer has been flushed.

Dotyczy

Zobacz też