Process.OutputDataReceived 事件

定義

發生於應用程式將某行寫入至其重新導向的 StandardOutput 資料流時。

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 

事件類型

屬性

範例

下列範例說明如何在命令的ipconfig重新導向StandardOutput數據流上執行異步讀取作業。

此範例會建立 OutputHandler 事件處理程式的事件委派,並將它與 OutputDataReceived 事件產生關聯。 事件處理程式會從重新導向 StandardOutput 的數據流接收文字行、格式化文字,並將它儲存在稍後顯示在範例控制台視窗中的輸出字串中。

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

備註

事件 OutputDataReceived 指出相關聯的 Process 已寫入換行字元 (換行字元 (換行字元, (CR) 、換行字元 (LF) ,或 CR+LF) 重新導向 StandardOutput 數據流。

事件會在上的 StandardOutput異步讀取作業期間啟用。 若要啟動異步讀取作業,您必須將 的Process資料流重新導向StandardOutput、將事件處理程式新增至 OutputDataReceived 事件,然後呼叫 BeginOutputReadLine。 之後, OutputDataReceived 每當進程將一行寫入重新導向 StandardOutput 數據流時,事件就會發出訊號,直到進程結束或呼叫 CancelOutputRead為止。

注意

正在處理異步輸出的應用程式應該呼叫 WaitForExit 方法,以確保已清除輸出緩衝區。

適用於

另請參閱