Process.StandardError Tulajdonság

Definíció

Lekéri az alkalmazás hibakimenetének olvasásához használt streamet.

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

Tulajdonság értéke

Az StreamReader alkalmazás szabványos hibastreamjének olvasására használható.

Attribútumok

Kivételek

A StandardError stream nincs definiálva átirányításhoz; győződjön meg arról RedirectStandardError , hogy true be van állítva és UseShellExecute be van állítva false.

-vagy-

A StandardError stream aszinkron olvasási műveletekhez lett megnyitva a következővel BeginErrorReadLine(): .

Példák

Az alábbi példa egy felhasználó által megadott argumentummal együtt használja a net use parancsot egy hálózati erőforrás leképezéséhez. Ezután beolvassa a net parancs szabványos hibafolyamát, és a konzolra írja.

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

Megjegyzések

Amikor egy Process szöveg írása a szokásos hibastreambe történik, az általában megjelenik a konzolon. A StandardError stream átirányításával módosíthatja vagy letilthatja egy folyamat hibakimenetét. Szűrheti például a szöveget, formázhatja másként, vagy a kimenetet a konzolra és a kijelölt naplófájlba is írhatja.

Note

A használathoz StandardErrorbe kell állítania a ProcessStartInfo.UseShellExecutekívánt false értéket, és be kell állítania a következőt ProcessStartInfo.RedirectStandardErrortrue: . Ellenkező esetben a StandardError streamből való olvasás kivételt eredményez.

Az átirányított StandardError stream szinkronizálva vagy aszinkron módon olvasható. Az olyan metódusok, mint a Read, ReadLineés ReadToEnd szinkron olvasási műveleteket hajtanak végre a folyamat kimeneti adatfolyamán. Ezek a szinkron olvasási műveletek addig nem fejeződnek be, amíg a társított Process írás nem fejeződik be a streambe StandardError , vagy nem zárja be a streamet.

Ezzel szemben BeginErrorReadLine elindítja az aszinkron olvasási műveleteket a StandardError streamen. Ez a módszer lehetővé teszi egy kijelölt eseménykezelőt a stream kimenetéhez, és azonnal visszatér a hívóhoz, amely más műveleteket is végrehajthat, miközben a stream kimenete az eseménykezelőhöz lesz irányítva.

A szinkron olvasási műveletek függőséget vezetnek be a hívó és a gyermekfolyamat között, amely az StandardError adott streambe ír. Ezek a függőségek holtponti állapotokat eredményezhetnek. Amikor a hívó egy gyermekfolyamat átirányított adatfolyamából olvas, az a gyermektől függ. A hívó megvárja az olvasási műveletet, amíg a gyermek be nem ír a streambe, vagy bezárja a streamet. Ha a gyermekfolyamat elegendő adatot ír az átirányított adatfolyam kitöltéséhez, az a szülőtől függ. A gyermekfolyamat megvárja a következő írási műveletet, amíg a szülő be nem olvas a teljes streamből, vagy bezárja a streamet. A holtpont állapota azt eredményezi, hogy a hívó és a gyermekfolyamat egymásra várva végrehajt egy műveletet, és egyik sem tud továbblépni. Elkerülheti a holtpontokat a hívó és a gyermekfolyamat közötti függőségek kiértékelésével.

Ebben a szakaszban az utolsó két példa egy Startnevű végrehajtható fájl elindítására használja a metódust. Az alábbi példa a forráskódját tartalmazza.

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.

Az alábbi példa bemutatja, hogyan olvasható egy átirányított hibastreamből, és várja meg, amíg a gyermekfolyamat kilép. Ez elkerüli a holtpont feltételt a hívás p.StandardError.ReadToEnd előtt p.WaitForExit. Holtpont-állapot akkor fordulhat elő, ha a szülőfolyamat korábban p.WaitForExit hív, p.StandardError.ReadToEnd és a gyermekfolyamat elegendő szöveget ír az átirányított stream kitöltéséhez. A szülőfolyamat határozatlan ideig várakozna, amíg a gyermekfolyamat kilép. A gyermekfolyamat határozatlan ideig várakozna, amíg a szülő felolvassa a teljes StandardError adatfolyamot.

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.

Hasonló probléma merül fel, ha az összes szöveget a standard kimenetből és a standard hibastreamekből is olvassa. Az alábbi példa olvasási műveletet hajt végre mindkét streamen. Elkerüli a holtpont állapotát a stream aszinkron olvasási StandardError műveleteinek végrehajtásával. A holtpont feltétel akkor jelenik meg, ha a szülőfolyamat meghívja p.StandardOutput.ReadToEnd őket, p.StandardError.ReadToEnd és a gyermekfolyamat elegendő szöveget ír a hibastream kitöltéséhez. A szülőfolyamat határozatlan ideig várakozna, amíg a gyermekfolyamat bezárja a streamet StandardOutput . A gyermekfolyamat határozatlan ideig várakozna, amíg a szülő felolvassa a teljes StandardError adatfolyamot.

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.

Aszinkron olvasási műveletekkel elkerülheti ezeket a függőségeket és azok holtpont-potenciálját. Alternatív megoldásként elkerülheti a holtpont feltételét úgy, hogy két szálat hoz létre, és beolvassa az egyes streamek kimenetét egy külön szálon.

Note

Átirányított streamen nem lehet aszinkron és szinkron olvasási műveleteket keverni. Ha egy átirányított stream Process aszinkron vagy szinkron módban van megnyitva, a streamen lévő összes további olvasási műveletnek ugyanabban a módban kell lennie. Például ne kövesse BeginErrorReadLine a streamre ReadLineStandardError irányuló hívásokat, vagy fordítva. Két különböző streamet azonban különböző módokon olvashat. Meghívhatja például, majd meghívhatja BeginOutputReadLineReadLine a streamet StandardError .

A következőre érvényes:

Lásd még