Process.OutputDataReceived 이벤트

정의

애플리케이션이 리디렉션된 StandardOutput 스트림에 쓸 때마다 발생합니다.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 

이벤트 유형

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.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 .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.

이 이벤트는에서 비동기 읽기 작업을 수행 하는 동안 사용 하도록 설정 됩니다 StandardOutput .The event is enabled during asynchronous read operations on StandardOutput. 비동기 읽기 작업을 시작 하려면의 스트림을 리디렉션하고 이벤트 StandardOutput Process 처리기를 이벤트에 추가 하 고를 호출 해야 합니다 OutputDataReceived 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. 그런 다음 프로세스가 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.

적용 대상

추가 정보