DataReceivedEventArgs 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
OutputDataReceived 및 ErrorDataReceived 이벤트에 대한 데이터를 제공합니다.
public ref class DataReceivedEventArgs : EventArgs
public class DataReceivedEventArgs : EventArgs
type DataReceivedEventArgs = class
inherit EventArgs
Public Class DataReceivedEventArgs
Inherits EventArgs
- 상속
예제
다음 코드 예제에서는 명령의 sort
리디렉션된 StandardOutput 스트림에서 비동기 읽기 작업을 수행하는 방법을 보여 줍니다. sort
명령 읽고 텍스트 입력을 정렬 하는 콘솔 애플리케이션입니다.
이 예제에서는 이벤트 처리기에 대한 이벤트 대리자를 SortOutputHandler
만들고 이벤트와 연결합니다 OutputDataReceived . 이벤트 처리기는 리디렉션된 StandardOutput 스트림에서 텍스트 줄을 수신하고, 텍스트의 서식을 지정하고, 텍스트를 화면에 씁니다.
// Define the namespaces used by this sample.
#using <System.dll>
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Diagnostics;
using namespace System::Threading;
using namespace System::ComponentModel;
ref class SortOutputRedirection
{
private:
// Define static variables shared by class methods.
static StringBuilder^ sortOutput = nullptr;
static int numOutputLines = 0;
public:
static void SortInputListText()
{
// Initialize the process and its StartInfo properties.
// The sort command is a console application that
// reads and sorts text input.
Process^ sortProcess;
sortProcess = gcnew Process;
sortProcess->StartInfo->FileName = "Sort.exe";
// Set UseShellExecute to false for redirection.
sortProcess->StartInfo->UseShellExecute = false;
// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess->StartInfo->RedirectStandardOutput = true;
sortOutput = gcnew StringBuilder;
// Set our event handler to asynchronously read the sort output.
sortProcess->OutputDataReceived += gcnew DataReceivedEventHandler( SortOutputHandler );
// Redirect standard input as well. This stream
// is used synchronously.
sortProcess->StartInfo->RedirectStandardInput = true;
// Start the process.
sortProcess->Start();
// Use a stream writer to synchronously write the sort input.
StreamWriter^ sortStreamWriter = sortProcess->StandardInput;
// Start the asynchronous read of the sort output stream.
sortProcess->BeginOutputReadLine();
// Prompt the user for input text lines. Write each
// line to the redirected input stream of the sort command.
Console::WriteLine( "Ready to sort up to 50 lines of text" );
String^ inputText;
int numInputLines = 0;
do
{
Console::WriteLine( "Enter a text line (or press the Enter key to stop):" );
inputText = Console::ReadLine();
if ( !String::IsNullOrEmpty( inputText ) )
{
numInputLines++;
sortStreamWriter->WriteLine( inputText );
}
}
while ( !String::IsNullOrEmpty( inputText ) && (numInputLines < 50) );
Console::WriteLine( "<end of input stream>" );
Console::WriteLine();
// End the input stream to the sort command.
sortStreamWriter->Close();
// Wait for the sort process to write the sorted text lines.
sortProcess->WaitForExit();
if ( numOutputLines > 0 )
{
// Write the formatted and sorted output to the console.
Console::WriteLine( " Sort results = {0} sorted text line(s) ",
numOutputLines.ToString() );
Console::WriteLine( "----------" );
Console::WriteLine( sortOutput->ToString() );
}
else
{
Console::WriteLine( " No input lines were sorted." );
}
sortProcess->Close();
}
private:
static void SortOutputHandler( Object^ /*sendingProcess*/,
DataReceivedEventArgs^ outLine )
{
// Collect the sort command output.
if ( !String::IsNullOrEmpty( outLine->Data ) )
{
numOutputLines++;
// Add the text to the collected output.
sortOutput->AppendFormat( "\n[{0}] {1}",
numOutputLines.ToString(), outLine->Data );
}
}
};
/// The main entry point for the application.
void main()
{
try
{
SortOutputRedirection::SortInputListText();
}
catch ( InvalidOperationException^ e )
{
Console::WriteLine( "Exception:" );
Console::WriteLine( e );
}
}
// Define the namespaces used by this sample.
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;
namespace ProcessAsyncStreamSamples
{
class SortOutputRedirection
{
// Define static variables shared by class methods.
private static StringBuilder sortOutput = null;
private static int numOutputLines = 0;
public static void SortInputListText()
{
// Initialize the process and its StartInfo properties.
// The sort command is a console application that
// reads and sorts text input.
Process sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";
// Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false;
// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = true;
sortOutput = new StringBuilder();
// Set our event handler to asynchronously read the sort output.
sortProcess.OutputDataReceived += SortOutputHandler;
// Redirect standard input as well. This stream
// is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = true;
// Start the process.
sortProcess.Start();
// Use a stream writer to synchronously write the sort input.
StreamWriter sortStreamWriter = sortProcess.StandardInput;
// Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine();
// Prompt the user for input text lines. Write each
// line to the redirected input stream of the sort command.
Console.WriteLine("Ready to sort up to 50 lines of text");
String inputText;
int numInputLines = 0;
do
{
Console.WriteLine("Enter a text line (or press the Enter key to stop):");
inputText = Console.ReadLine();
if (!String.IsNullOrEmpty(inputText))
{
numInputLines++;
sortStreamWriter.WriteLine(inputText);
}
}
while (!String.IsNullOrEmpty(inputText) && (numInputLines < 50));
Console.WriteLine("<end of input stream>");
Console.WriteLine();
// End the input stream to the sort command.
sortStreamWriter.Close();
// Wait for the sort process to write the sorted text lines.
sortProcess.WaitForExit();
if (numOutputLines > 0)
{
// Write the formatted and sorted output to the console.
Console.WriteLine($" Sort results = {numOutputLines} sorted text line(s) ");
Console.WriteLine("----------");
Console.WriteLine(sortOutput);
}
else
{
Console.WriteLine(" No input lines were sorted.");
}
sortProcess.Close();
}
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
numOutputLines++;
// Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
$"[{numOutputLines}] - {outLine.Data}");
}
}
}
}
namespace ProcessAsyncStreamSamples
{
class ProcessSampleMain
{
/// The main entry point for the application.
static void Main()
{
try
{
SortOutputRedirection.SortInputListText();
}
catch (InvalidOperationException e)
{
Console.WriteLine("Exception:");
Console.WriteLine(e);
}
}
}
}
' Define the namespaces used by this sample.
Imports System.Text
Imports System.IO
Imports System.Diagnostics
Imports System.Threading
Imports System.ComponentModel
Namespace ProcessAsyncStreamSamples
Class ProcessAsyncOutputRedirection
' Define static variables shared by class methods.
Private Shared sortOutput As StringBuilder = Nothing
Private Shared numOutputLines As Integer = 0
Public Shared Sub SortInputListText()
' Initialize the process and its StartInfo properties.
' The sort command is a console application that
' reads and sorts text input.
Dim sortProcess As New Process()
sortProcess.StartInfo.FileName = "Sort.exe"
' Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = False
' Redirect the standard output of the sort command.
' Read the stream asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = True
sortOutput = New StringBuilder()
' Set our event handler to asynchronously read the sort output.
AddHandler sortProcess.OutputDataReceived, AddressOf SortOutputHandler
' Redirect standard input as well. This stream
' is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = True
' Start the process.
sortProcess.Start()
' Use a stream writer to synchronously write the sort input.
Dim sortStreamWriter As StreamWriter = sortProcess.StandardInput
' Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine()
' Prompt the user for input text lines. Write each
' line to the redirected input stream of the sort command.
Console.WriteLine("Ready to sort up to 50 lines of text")
Dim inputText As String
Dim numInputLines As Integer = 0
Do
Console.WriteLine("Enter a text line (or press the Enter key to stop):")
inputText = Console.ReadLine()
If Not String.IsNullOrEmpty(inputText) Then
numInputLines += 1
sortStreamWriter.WriteLine(inputText)
End If
Loop While Not String.IsNullOrEmpty(inputText) AndAlso numInputLines < 50
Console.WriteLine("<end of input stream>")
Console.WriteLine()
' End the input stream to the sort command.
sortStreamWriter.Close()
' Wait for the sort process to write the sorted text lines.
sortProcess.WaitForExit()
If Not String.IsNullOrEmpty(numOutputLines) Then
' Write the formatted and sorted output to the console.
Console.WriteLine($" Sort results = {numOutputLines} sorted text line(s) ")
Console.WriteLine("----------")
Console.WriteLine(sortOutput)
Else
Console.WriteLine(" No input lines were sorted.")
End If
sortProcess.Close()
End Sub
Private Shared Sub SortOutputHandler(sendingProcess As Object,
outLine As DataReceivedEventArgs)
' Collect the sort command output.
If Not String.IsNullOrEmpty(outLine.Data) Then
numOutputLines += 1
' Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
$"[{numOutputLines}] - {outLine.Data}")
End If
End Sub
End Class
End Namespace
Namespace ProcessAsyncStreamSamples
Class ProcessSampleMain
' The main entry point for the application.
Shared Sub Main()
Try
ProcessAsyncOutputRedirection.SortInputListText()
Catch e As InvalidOperationException
Console.WriteLine("Exception:")
Console.WriteLine(e)
End Try
End Sub
End Class 'ProcessSampleMain
End Namespace 'Process_AsyncStream_Sample
설명
프로세스의 리디렉션된 또는 StandardError 스트림 출력을 비동기적으로 수집하려면 리디렉션된 StandardOutput 스트림 출력 이벤트를 처리하는 메서드를 만들어야 합니다. 이벤트 처리기 메서드는 프로세스가 리디렉션된 스트림에 쓸 때 호출됩니다. 이벤트 대리자는 의 instance 사용하여 이벤트 처리기를 호출합니다DataReceivedEventArgs. 속성에는 Data 프로세스가 리디렉션된 스트림에 쓴 텍스트 줄이 포함됩니다.
속성
Data |
리디렉션된 Process 출력 스트림에 쓰여진 문자 줄을 가져옵니다. |
메서드
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다. (다음에서 상속됨 Object) |
GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
GetType() |
현재 인스턴스의 Type을 가져옵니다. (다음에서 상속됨 Object) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
적용 대상
추가 정보
.NET