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

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

ProcessStartInfo.RedirectStandardOutput プロパティ

アプリケーション出力を Process.StandardOutput ストリーム書き込むかどうかを示す値を取得または設定します

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

Public Property RedirectStandardOutput As
 Boolean
Dim instance As ProcessStartInfo
Dim value As Boolean

value = instance.RedirectStandardOutput

instance.RedirectStandardOutput = value
public bool RedirectStandardOutput { get;
 set; }
public:
property bool RedirectStandardOutput {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_RedirectStandardOutput ()

/** @property */
public void set_RedirectStandardOutput (boolean
 value)
public function get RedirectStandardOutput
 () : boolean

public function set RedirectStandardOutput
 (value : boolean)

プロパティ
出力Process.StandardOutput書き込む場合trueそれ以外場合false

解説解説

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

メモメモ

RedirectStandardOutputtrue設定するには、UseShellExecute を false設定する必要があります設定されていない場合は、StandardOutput ストリームからの読み取りを行うと例外スローさます。

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

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

同期読み取り操作により、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 つスレッド作成して個別スレッドで各ストリーム出力読み取る方法あります

使用例使用例
compiler.StartInfo.FileName = "csc.exe"
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe
 stdstr.cs"
compiler.StartInfo.UseShellExecute = False
compiler.StartInfo.RedirectStandardOutput = True
compiler.Start()

Console.WriteLine(compiler.StandardOutput.ReadToEnd())

compiler.WaitForExit()
Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();    

Console.WriteLine(compiler.StandardOutput.ReadToEnd());

compiler.WaitForExit();
Process^ compiler = gcnew Process;
compiler->StartInfo->FileName = "cl.exe";
compiler->StartInfo->Arguments = "/clr stdstr.cpp /link /out:sample.exe";
compiler->StartInfo->UseShellExecute = false;
compiler->StartInfo->RedirectStandardOutput = true;
compiler->Start();

Console::WriteLine( compiler->StandardOutput->ReadToEnd() );

compiler->WaitForExit();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ProcessStartInfo クラス
ProcessStartInfo メンバ
System.Diagnostics 名前空間
UseShellExecute
Process.StandardOutput プロパティ


このページでは「.NET Framework クラス ライブラリ リファレンス」からProcessStartInfo.RedirectStandardOutput プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からProcessStartInfo.RedirectStandardOutput プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からProcessStartInfo.RedirectStandardOutput プロパティ を検索

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

辞書ショートカット

すべての辞書の索引

「ProcessStartInfo.RedirectStandardOutput プロパティ」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS