DataReceivedEventArgs クラス
アセンブリ: System (system.dll 内)


プロセスのリダイレクトされた StandardOutput または StandardError ストリーム出力を非同期に収集するには、リダイレクトされたストリーム出力イベントを処理するメソッドを作成する必要があります。プロセスがリダイレクトされたストリームに書き込むときに、イベント ハンドラ メソッドが呼び出されます。イベント デリゲートは、DataReceivedEventArgs のインスタンスと共にイベント ハンドラを呼び出します。Data プロパティには、プロセスがリダイレクトされたストリームに書き込んだテキスト行が格納されます。

sort コマンドのリダイレクトされた StandardOutput ストリームで、非同期読み取り操作を実行するコード例を次に示します。sort コマンドは、テキスト入力を読み込み、並べ替えを実行するコンソール アプリケーションです。
この例では、SortOutputHandler イベント ハンドラのイベント デリゲートを作成して、OutputDataReceived イベントに関連付けます。イベント ハンドラは、リダイレクトされた StandardOutput ストリームからテキスト行を受け取った後、そのテキストに書式を設定して画面に出力します。
' Define the namespaces used by this sample. Imports System Imports System.Text Imports System.IO Imports System.Diagnostics Imports System.Threading Imports System.ComponentModel Imports Microsoft.VisualBasic 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 = {0} sorted text line(s) ", _ numOutputLines) 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.ToString() + "] - " _ + outLine.Data) End If End Sub End Class End Namespace
// 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; 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 += new 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); 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.ToString() + "] - " + outLine.Data); } } } }
// 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 ); } } };

System.EventArgs
System.Diagnostics.DataReceivedEventArgs


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataReceivedEventArgs メンバ
System.Diagnostics 名前空間
DataReceivedEventHandler
Process.OutputDataReceived
Process.ErrorDataReceived
System.Diagnostics.Process
DataReceivedEventArgs プロパティ


関連項目
DataReceivedEventArgs クラスSystem.Diagnostics 名前空間
DataReceivedEventHandler
Process.OutputDataReceived
Process.ErrorDataReceived
System.Diagnostics.Process
DataReceivedEventArgs メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

関連項目
DataReceivedEventArgs クラスSystem.Diagnostics 名前空間
DataReceivedEventHandler
Process.OutputDataReceived
Process.ErrorDataReceived
System.Diagnostics.Process
DataReceivedEventArgs メンバ
OutputDataReceived イベントと ErrorDataReceived イベントのデータを提供します。
DataReceivedEventArgs データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

関連項目
DataReceivedEventArgs クラスSystem.Diagnostics 名前空間
DataReceivedEventHandler
Process.OutputDataReceived
Process.ErrorDataReceived
System.Diagnostics.Process
- DataReceivedEventArgsのページへのリンク