Process.StandardError プロパティ

定義

アプリケーションのエラー出力を読み取るために使用されるストリームを取得します。

public:
 property System::IO::StreamReader ^ StandardError { System::IO::StreamReader ^ get(); };
public System.IO.StreamReader StandardError { get; }
[System.ComponentModel.Browsable(false)]
public System.IO.StreamReader StandardError { get; }
member this.StandardError : System.IO.StreamReader
[<System.ComponentModel.Browsable(false)>]
member this.StandardError : System.IO.StreamReader
Public ReadOnly Property StandardError As StreamReader

プロパティ値

アプリケーションの標準エラー ストリームを読み取るために使用できる StreamReader

属性

例外

StandardError ストリームはリダイレクト用に定義されていません。RedirectStandardErrortrue に設定され、UseShellExecutefalse に設定されていることを確認してください。

-又は-

StandardError ストリームは、BeginErrorReadLine()を使用した非同期読み取り操作のために開かれています。

次の例では、 net use コマンドをユーザー指定の引数と共に使用して、ネットワーク リソースをマップします。 その後、net コマンドの標準エラー ストリームを読み取り、コンソールに書き込みます。

using (Process myProcess = new Process())
{
    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ", "use " + args[0]);

    myProcessStartInfo.UseShellExecute = false;
    myProcessStartInfo.RedirectStandardError = true;
    myProcess.StartInfo = myProcessStartInfo;
    myProcess.Start();

    StreamReader myStreamReader = myProcess.StandardError;
    // Read the standard error of net.exe and write it on to console.
    Console.WriteLine(myStreamReader.ReadLine());
}
use myProcess = new Process()
let myProcessStartInfo = ProcessStartInfo("net ", $"use {args[0]}")

myProcessStartInfo.UseShellExecute <- false
myProcessStartInfo.RedirectStandardError <- true
myProcess.StartInfo <- myProcessStartInfo
myProcess.Start() |> ignore

let myStreamReader = myProcess.StandardError
// Read the standard error of net.exe and write it on to console.
printfn $"{myStreamReader.ReadLine()}"
Using myProcess As New Process()
    Dim myProcessStartInfo As New ProcessStartInfo("net ", "use " + args(0))

    myProcessStartInfo.UseShellExecute = False
    myProcessStartInfo.RedirectStandardError = True
    myProcess.StartInfo = myProcessStartInfo
    myProcess.Start()

    Dim myStreamReader As StreamReader = myProcess.StandardError
    ' Read the standard error of net.exe and write it on to console.
    Console.WriteLine(myStreamReader.ReadLine())
End Using

注釈

Processが標準エラー ストリームにテキストを書き込むと、そのテキストは通常コンソールに表示されます。 StandardError ストリームをリダイレクトすることで、プロセスのエラー出力を操作または抑制できます。 たとえば、テキストをフィルター処理したり、別の形式で書式設定したり、コンソールと指定されたログ ファイルの両方に出力を書き込んだりできます。

Note

StandardErrorを使用するには、ProcessStartInfo.UseShellExecutefalse に設定し、ProcessStartInfo.RedirectStandardErrortrue に設定する必要があります。 それ以外の場合、 StandardError ストリームからの読み取りでは例外がスローされます。

リダイレクトされた StandardError ストリームは、同期的または非同期的に読み取ることができます。 ReadReadLineReadToEndなどのメソッドは、プロセスのエラー出力ストリームに対して同期読み取り操作を実行します。 これらの同期読み取り操作は、関連付けられている ProcessStandardError ストリームに書き込むか、ストリームを閉じるまで完了しません。

これに対し、 BeginErrorReadLine は、 StandardError ストリームに対する非同期読み取り操作を開始します。 このメソッドは、ストリーム出力に対して指定されたイベント ハンドラーを有効にし、すぐに呼び出し元に戻ります。呼び出し元は、ストリーム出力がイベント ハンドラーに送信されている間に他の処理を実行できます。

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

このセクションの最後の 2 つの例では、 Start メソッドを使用して 、Write500Lines.exeという名前の実行可能ファイルを起動します。 次の例には、そのソース コードが含まれています。

using System;

public class Example3
{
    public static void Main()
    {
        for (int ctr = 0; ctr < 500; ctr++)
            Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}");

        Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n");
    }
}
// The example displays the following output:
//      Line 1 of 500 written: 0,20%
//      Line 2 of 500 written: 0,40%
//      Line 3 of 500 written: 0,60%
//      ...
//      Line 498 of 500 written: 99,60%
//      Line 499 of 500 written: 99,80%
//      Line 500 of 500 written: 100,00%
//
//      Successfully wrote 500 lines.
module Write500Lines

for i in 1. .. 500. do
    printfn $"Line {i} of 500 written: {i/500.:P2}";

eprintfn "Successfully wrote 500 lines.";
// The example displays the following output:
//      Line 1 of 500 written: 0,20%
//      Line 2 of 500 written: 0,40%
//      Line 3 of 500 written: 0,60%
//      ...
//      Line 498 of 500 written: 99,60%
//      Line 499 of 500 written: 99,80%
//      Line 500 of 500 written: 100,00%
//      Successfully wrote 500 lines.
Imports System.IO

Public Module Example
   Public Sub Main()
      For ctr As Integer = 0 To 499
         Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}")
      Next

      Console.Error.WriteLine($"{vbCrLf}Successfully wrote 500 lines.{vbCrLf}")
   End Sub
End Module
' The example displays the following output:
'      Line 1 of 500 written 0,20%
'      Line 2 of 500 written: 0,40%
'      Line 3 of 500 written: 0,60%
'      ...
'      Line 498 of 500 written: 99,60%
'      Line 499 of 500 written: 99,80%
'      Line 500 of 500 written: 100,00%
'
'      Successfully wrote 500 lines.

次の例は、リダイレクトされたエラー ストリームから読み取り、子プロセスが終了するまで待機する方法を示しています。 p.StandardError.ReadToEndする前にp.WaitForExitを呼び出すことでデッドロック状態を回避します。 親プロセスがp.WaitForExitする前にp.StandardError.ReadToEndを呼び出し、子プロセスがリダイレクトされたストリームを埋めるのに十分なテキストを書き込む場合、デッドロック状態が発生する可能性があります。 親プロセスは、子プロセスが終了するまで無期限に待機します。 子プロセスは、親が完全な StandardError ストリームから読み取るのを無期限に待機します。

using System;
using System.Diagnostics;

public class Example
{
   public static void Main()
   {
      var p = new Process();  
      p.StartInfo.UseShellExecute = false;  
      p.StartInfo.RedirectStandardError = true;  
      p.StartInfo.FileName = "Write500Lines.exe";  
      p.Start();  

      // To avoid deadlocks, always read the output stream first and then wait.  
      string output = p.StandardError.ReadToEnd();  
      p.WaitForExit();

      Console.WriteLine($"\nError stream: {output}");
   }
}
// The end of the output produced by the example includes the following:
//      Error stream:
//      Successfully wrote 500 lines.
module STDErrorSync

open System.Diagnostics

let p = new Process()
p.StartInfo.UseShellExecute <- false
p.StartInfo.RedirectStandardError <- true
p.StartInfo.FileName <- "Write500Lines.exe"
p.Start() |> ignore

// To avoid deadlocks, always read the output stream first and then wait.
let output = p.StandardError.ReadToEnd()
p.WaitForExit()

printfn $"\nError stream: {output}"
// The end of the output produced by the example includes the following:
//      Error stream:
//      Successfully wrote 500 lines.
Imports System.Diagnostics

Public Module Example
    Public Sub Main()
        Dim p As New Process()
        p.StartInfo.UseShellExecute = False  
        p.StartInfo.RedirectStandardError = True  
        p.StartInfo.FileName = "Write500Lines.exe"  
        p.Start() 

        ' To avoid deadlocks, always read the output stream first and then wait.  
        Dim output As String = p.StandardError.ReadToEnd()  
        p.WaitForExit()

        Console.WriteLine($"{vbCrLf}Error stream: {output}")
    End Sub
End Module
' The end of the output produced by the example includes the following:
'      Error stream:
'      Successfully wrote 500 lines.

標準出力ストリームと標準エラー ストリームの両方からすべてのテキストを読み取ると、同様の問題があります。 次の例では、両方のストリームで読み取り操作を実行します。 StandardError ストリームに対して非同期読み取り操作を実行することで、デッドロック状態を回避します。 デッドロック状態は、親プロセスが p.StandardOutput.ReadToEnd を呼び出し、その後に p.StandardError.ReadToEnd し、子プロセスがエラー ストリームを埋めるのに十分なテキストを書き込む場合に発生します。 親プロセスは、子プロセスが StandardOutput ストリームを閉じるのを無期限に待機します。 子プロセスは、親が完全な StandardError ストリームから読み取るのを無期限に待機します。

using System;
using System.Diagnostics;

public class Example
{
    public static void Main()
    {
        var p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        string eOut = null;
        p.StartInfo.RedirectStandardError = true;
        p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
                                   { eOut += e.Data; });
        p.StartInfo.FileName = "Write500Lines.exe";
        p.Start();

        // To avoid deadlocks, use an asynchronous read operation on at least one of the streams.  
        p.BeginErrorReadLine();
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
        Console.WriteLine($"\nError stream: {eOut}");
    }
}
// The example displays the following output:
//      The last 50 characters in the output stream are:
//      'ritten: 99,80%
//      Line 500 of 500 written: 100,00%
//      '
//
//      Error stream: Successfully wrote 500 lines.
module STDOutputAsync

open System.Diagnostics

let p = new Process()
p.StartInfo.UseShellExecute <- false
p.StartInfo.RedirectStandardOutput <- true
let mutable eOut = ""
p.StartInfo.RedirectStandardError <- true
p.ErrorDataReceived.AddHandler(DataReceivedEventHandler(fun sender e -> eOut <- eOut + e.Data))

p.StartInfo.FileName <- "Write500Lines.exe"
p.Start() |> ignore

// To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
p.BeginErrorReadLine()
let output = p.StandardOutput.ReadToEnd()
p.WaitForExit()

printfn $"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'"
printfn $"\nError stream: {eOut}"
// The example displays the following output:
//      The last 50 characters in the output stream are:
//      'ritten: 99,80%
//      Line 500 of 500 written: 100,00%
//      '
//
//      Error stream: Successfully wrote 500 lines.
Imports System.Diagnostics

Public Module Example
   Public Sub Main()
      Dim p As New Process()  
      p.StartInfo.UseShellExecute = False  
      p.StartInfo.RedirectStandardOutput = True  
      Dim eOut As String = Nothing
      p.StartInfo.RedirectStandardError = True
      AddHandler p.ErrorDataReceived, Sub(sender, e) eOut += e.Data 
      p.StartInfo.FileName = "Write500Lines.exe"  
      p.Start()  

      ' To avoid deadlocks, use an asynchronous read operation on at least one of the streams.  
      p.BeginErrorReadLine()
      Dim output As String = p.StandardOutput.ReadToEnd()  
      p.WaitForExit()

      Console.WriteLine($"The last 50 characters in the output stream are:{vbCrLf}'{output.Substring(output.Length - 50)}'")
      Console.WriteLine($"{vbCrLf}Error stream: {eOut}")
   End Sub
End Module
' The example displays the following output:
'      The last 50 characters in the output stream are:
'      'ritten: 99,80%
'      Line 500 of 500 written: 100,00%
'      '
'
'      Error stream: Successfully wrote 500 lines.

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

Note

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

適用対象

こちらもご覧ください