Process.OutputDataReceived Process.OutputDataReceived Process.OutputDataReceived Process.OutputDataReceived Event

定義

アプリケーションが、リダイレクトされた StandardOutput ストリームに行を書き込む度に発生します。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 Custom Event OutputDataReceived As DataReceivedEventHandler 
属性

次の例は、 StandardOutput ipconfigコマンドのリダイレクトされたストリームで非同期の読み取り操作を実行する方法を示しています。The following example illustrates how to perform asynchronous read operations on the redirected StandardOutput stream of the ipconfig command.

この例では、イベントハンドラーのOutputHandlerイベントデリゲートを作成し、イベントOutputDataReceivedに関連付けます。The example creates an event delegate for the OutputHandler event handler and associates it with the OutputDataReceived event. イベントハンドラーは、リダイレクトStandardOutputされたストリームからテキスト行を受け取り、テキストを書式設定して、後で例のコンソールウィンドウに表示される出力文字列に保存します。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
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れたが、リダイレクトStandardOutputされたストリームの改行文字で終了する行を書き込んだことを示します。The OutputDataReceived event indicates that the associated Process has written a line, terminating with a newline character, to its redirected StandardOutput stream.

イベントは、でのStandardOutput非同期読み取り操作中に有効になります。The event is enabled during asynchronous read operations on StandardOutput. 非同期StandardOutput OutputDataReceived の読み取りBeginOutputReadLine操作を開始するには、のストリームをリダイレクトし、イベントにイベントハンドラーを追加して、を呼び出す必要があります。ProcessTo start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. その後、 OutputDataReceivedこのイベントは、プロセスが終了するか、が呼び出さStandardOutput 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.

注意

非同期出力を処理するアプリケーションでは、出力バッファー WaitForExitがフラッシュされたことを確認するためにメソッドを呼び出す必要があります。The application that is processing the asynchronous output should call the WaitForExit method to ensure that the output buffer has been flushed.

セキュリティ

LinkDemand
直前の呼び出し元に対する完全な信頼。for full trust for the immediate caller. このメンバーは、部分的に信頼されているコードから使用することはできません。This member cannot be used by partially trusted code.

適用対象

こちらもご覧ください