Process.StandardOutput プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Process.StandardOutput プロパティの意味・解説 

Process.StandardOutput プロパティ

アプリケーション出力読み取り使用されるストリーム取得します

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

Dim instance As Process
Dim value As StreamReader

value = instance.StandardOutput
public StreamReader StandardOutput { get; }
public:
property StreamReader^ StandardOutput {
    StreamReader^ get ();
}
/** @property */
public StreamReader get_StandardOutput ()

プロパティ
アプリケーション標準出力ストリーム読み取り使用できる StreamReader。

例外例外
例外種類条件

InvalidOperationException

StandardOutput ストリームリダイレクト用に定義されていません。ProcessStartInfo.RedirectStandardOutput が true設定され、ProcessStartInfo.UseShellExecute が false設定されていることを確認してください

または

StandardOutput ストリームは、BeginOutputReadLine による非同期読み取り操作のために開かれています。

解説解説

Processテキスト標準ストリーム書き込むと、通常、そのテキストコンソール表示されます。StandardOutput ストリームリダイレクトすることにより、プロセス出力操作したり、非表示にしたりできます。たとえば、テキストフィルタ処理異な書式設定適用、またはコンソールおよび指定したログ ファイル両方への出力書き込み実行できます

メモメモ

StandardOutput使用するには、ProcessStartInfo.UseShellExecutefalse設定しProcessStartInfo.RedirectStandardOutputtrue設定する必要があります設定しない場合は、StandardOutput ストリーム書き込みを行うと例外スローさます。

リダイレクトされた StandardOutput ストリーム読み取りは、同期または非同期実行できますReadReadLine、および ReadToEnd のようなメソッドは、プロセス出力ストリーム同期読み取り操作実行します。これらの同期読み取り操作は、関連する Process がその StandardOutput ストリーム書き込むか、ストリーム閉じるまで完了しません。

対照的にBeginOutputReadLineStandardOutput ストリーム非同期読み取り操作開始します。このメソッドは、ストリーム出力指定されイベント ハンドラ有効にした後、直ち呼び出し元に制御返します。これにより、呼び出し元は、ストリーム出力イベント ハンドラリダイレクトされている間に他の作業実行できます

同期読み取り操作により、StandardOutput ストリームから読み取る呼び出し元と、そのストリーム書き込む子プロセスの間に依存関係発生します。この依存関係によってデッドロック状態が発生する場合あります呼び出し元が子プロセスリダイレクトされたストリームから読み取る場合呼び出し元は子に依存します。子がストリーム書き込むまで、またはストリーム閉じるまで、呼び出し元は読み取り操作待機します。子プロセスから書き込まれデータによってリダイレクトされたストリーム満杯になる場合子プロセスは親に依存します子プロセスは、満杯になったストリームから親がデータ読み取るまで、またはストリーム閉じるまで、次の書き込み操作待機します。デッドロック状態では、呼び出し元および子プロセス互いに操作完了するまで待機するので、どちらも操作続行できません。デッドロック回避するには、呼び出し元と子プロセスとの依存関係検査します

リダイレクトされたストリームから読み取り子プロセス終了するまで待機する方法を、次の C# コード例示します

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

このコード例では、p.WaitForExit前に p.StandardOutput.ReadToEnd呼び出してデッドロック回避してます。親プロセスp.StandardOutput.ReadToEnd前に p.WaitForExit呼び出しリダイレクトされたストリーム満杯になるまで子プロセステキスト書き込む場合デッドロック状態が発生する可能性あります親プロセスは、子プロセス終了するまで無期限待機することになります子プロセスは、満杯になった StandardOutput ストリームから親がデータ読み取るまで、無期限待機することになります

標準出力および標準エラー ストリーム両方からすべてのテキスト読み取る場合にも類似の問題発生します両方ストリーム読み取り操作実行する C# コード例次に示します

 // Do not perform a synchronous read to the end of both 
 // redirected streams.
 // string output = p.StandardOutput.ReadToEnd();
 // string error = p.StandardError.ReadToEnd();
 // p.WaitForExit();
 // Use asynchronous read operations on at least one of the streams.
 p.BeginOutputReadLine();
 string error = p.StandardError.ReadToEnd();
 p.WaitForExit();

このコード例では、StandardOutput ストリーム非同期読み取り操作実行してデッドロック状態を回避してます。親プロセスp.StandardOutput.ReadToEnd の後に p.StandardError.ReadToEnd呼び出しエラー ストリーム満杯になるまで子プロセステキスト書き込む場合デッドロック状態が発生する可能性あります親プロセスは、子プロセスStandardOutput ストリーム閉じるまで無期限待機することになります子プロセスは、満杯になった StandardError ストリームから親がデータ読み取るまで、無期限待機することになります

非同期読み取り操作使用すると、これらの依存関係およびデッドロック可能性回避できますデッドロック状態を回避するには、2 つスレッド作成して個別スレッドで各ストリーム出力読み取る方法あります

メモメモ

リダイレクトされたストリームで、非同期読み取り操作同期読み取り操作混在させることはできません。Processリダイレクトされたストリーム非同期モードまたは同期モード開いた後は、それ以降、そのストリーム対すすべての読み取り操作は同じモードで行う必要があります。たとえば、StandardOutputBeginOutputReadLine の後に ReadLine呼び出し続けないください。逆も同様です。ただし、2 つ異なストリームそれぞれ別のモード読み取ることはできます。たとえば、BeginOutputReadLine呼び出した後でStandardError ストリーム用に ReadLine呼び出すことができます

使用例使用例

ユーザー定義の新し実行可能ファイル作成し、その標準出力読み取る例を次に示します読み取った出力は、コンソール表示します

Dim myProcess As New Process()
Dim myProcessStartInfo As New
 ProcessStartInfo("Process_StandardOutput_Sample.exe")
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardOutput = True
myProcess.StartInfo = myProcessStartInfo
myProcess.Start()

Dim myStreamReader As StreamReader = myProcess.StandardOutput
' Read the standard output of the spawned process.
Dim myString As String =
 myStreamReader.ReadLine()
Console.WriteLine(myString)
myProcess.Close()
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("Process_StandardOutput_Sample.exe"
 );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
myProcess.Close();
Process^ myProcess = gcnew Process;
ProcessStartInfo^ myProcessStartInfo = gcnew ProcessStartInfo(
   "Process_StandardOutput_Sample.exe" );
myProcessStartInfo->UseShellExecute = false;
myProcessStartInfo->RedirectStandardOutput = true;
myProcess->StartInfo = myProcessStartInfo;
myProcess->Start();

StreamReader^ myStreamReader = myProcess->StandardOutput;
// Read the standard output of the spawned process.
String^ myString = myStreamReader->ReadLine();
Console::WriteLine( myString );
myProcess->Close();
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Process クラス
Process メンバ
System.Diagnostics 名前空間
Process.StandardInput プロパティ
Process.StandardError プロパティ
ProcessStartInfo.RedirectStandardOutput



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Process.StandardOutput プロパティ」の関連用語

Process.StandardOutput プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Process.StandardOutput プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS