Process.OutputDataReceived 이벤트
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
애플리케이션이 리디렉션된 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 메서드를 출력 버퍼를 플러시 했습니다.
적용 대상
추가 정보
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기