Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Propiedad Process.StandardOutput

 

Publicado: noviembre de 2016

Obtiene una secuencia usada para leer la salida de texto de la aplicación.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

<BrowsableAttribute(False)>
Public ReadOnly Property StandardOutput As StreamReader

Valor de propiedad

Type: System.IO.StreamReader

StreamReader que puede usarse para leer la secuencia de salida estándar de la aplicación.

Exception Condition
InvalidOperationException

La secuencia StandardOutput no se ha definido para redirección; asegúrese de que ProcessStartInfo.RedirectStandardOutput se establece en true y ProcessStartInfo.UseShellExecute que se establece en false.

o bien

La secuencia StandardOutput se ha abierto para operaciones de lectura asincrónicas con BeginOutputReadLine.

When a T:System.Diagnostics.Process writes text to its standard stream, that text is normally displayed on the console. By redirecting the P:System.Diagnostics.Process.StandardOutput stream, you can manipulate or suppress the output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file.

System_CAPS_noteNota

To use P:System.Diagnostics.Process.StandardOutput, you must set P:System.Diagnostics.ProcessStartInfo.UseShellExecute to false, and you must set P:System.Diagnostics.ProcessStartInfo.RedirectStandardOutput to true. Otherwise, reading from the P:System.Diagnostics.Process.StandardOutput stream throws an exception.

The redirected P:System.Diagnostics.Process.StandardOutput stream can be read synchronously or asynchronously. Methods such as M:System.IO.StreamReader.Read, M:System.IO.StreamReader.ReadLine, and M:System.IO.StreamReader.ReadToEnd perform synchronous read operations on the output stream of the process. These synchronous read operations do not complete until the associated T:System.Diagnostics.Process writes to its P:System.Diagnostics.Process.StandardOutput stream, or closes the stream.

In contrast, M:System.Diagnostics.Process.BeginOutputReadLine starts asynchronous read operations on the P:System.Diagnostics.Process.StandardOutput stream. This method enables a designated event handler for the stream output and immediately returns to the caller, which can perform other work while the stream output is directed to the event handler.

Synchronous read operations introduce a dependency between the caller reading from the P:System.Diagnostics.Process.StandardOutput stream and the child process writing to that stream. These dependencies can result in deadlock conditions. When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits on the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits on the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait on each other to complete an operation, and neither can proceed. You can avoid deadlocks by evaluating dependencies between the caller and child process.

The following C# code, for example, shows how to read from a redirected stream and wait for the child process to exit.

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();

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

The code example avoids a deadlock condition by calling p.StandardOutput.ReadToEnd before p.WaitForExit. A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full P:System.Diagnostics.Process.StandardOutput stream.

There is a similar issue when you read all text from both the standard output and standard error streams. The following C# code, for example, performs a read operation on both streams.

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

The code example avoids the deadlock condition by performing asynchronous read operations on the P:System.Diagnostics.Process.StandardOutput stream. A deadlock condition results if the parent process calls p.StandardOutput.ReadToEnd followed by p.StandardError.ReadToEnd and the child process writes enough text to fill its error stream. The parent process would wait indefinitely for the child process to close its P:System.Diagnostics.Process.StandardOutput stream. The child process would wait indefinitely for the parent to read from the full P:System.Diagnostics.Process.StandardError stream.

You can use asynchronous read operations to avoid these dependencies and their deadlock potential. Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread.

System_CAPS_noteNota

You cannot mix asynchronous and synchronous read operations on a redirected stream. Once the redirected stream of a T:System.Diagnostics.Process is opened in either asynchronous or synchronous mode, all further read operations on that stream must be in the same mode. For example, do not follow M:System.Diagnostics.Process.BeginOutputReadLine with a call to M:System.IO.StreamReader.ReadLine on the P:System.Diagnostics.Process.StandardOutput stream, or vice versa. However, you can read two different streams in different modes. For example, you can call M:System.Diagnostics.Process.BeginOutputReadLine and then call M:System.IO.StreamReader.ReadLine for the P:System.Diagnostics.Process.StandardError stream.

The following example runs the ipconfig.exe command and redirects its standard output to the example’s console window.

Imports System
Imports System.IO
Imports System.Diagnostics

Module Module1
    Sub Main()
        Dim process As New Process()
        process.StartInfo.FileName = "ipconfig.exe"
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardOutput = True
        process.Start()

        ' Synchronously read the standard output of the spawned process. 
        Dim reader As StreamReader = process.StandardOutput
        Dim output As String = reader.ReadToEnd()
        Console.WriteLine(output)

        process.WaitForExit()
        process.Close()

        Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
        Console.ReadLine()
    End Sub
End Module

LinkDemand

for full trust for the immediate caller. This member cannot be used by partially trusted code.

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: