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

Evento Process.OutputDataReceived

 

Publicado: octubre de 2016

Se produce cada vez que una aplicación escribe una línea en su secuencia StandardOutput redirigida.

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

[BrowsableAttribute(true)]
public event DataReceivedEventHandler OutputDataReceived

The E:System.Diagnostics.Process.OutputDataReceived event indicates that the associated T:System.Diagnostics.Process has written a line, terminating with a newline character, to its redirected P:System.Diagnostics.Process.StandardOutput stream.

The event is enabled during asynchronous read operations on P:System.Diagnostics.Process.StandardOutput. To start asynchronous read operations, you must redirect the P:System.Diagnostics.Process.StandardOutput stream of a T:System.Diagnostics.Process, add your event handler to the E:System.Diagnostics.Process.OutputDataReceived event, and call M:System.Diagnostics.Process.BeginOutputReadLine. Thereafter, the E:System.Diagnostics.Process.OutputDataReceived event signals each time the process writes a line to the redirected P:System.Diagnostics.Process.StandardOutput stream, until the process exits or calls M:System.Diagnostics.Process.CancelOutputRead.

System_CAPS_noteNota

The application that is processing the asynchronous output should call the M:System.Diagnostics.Process.WaitForExit method to ensure that the output buffer has been flushed.

The following example illustrates how to perform asynchronous read operations on the redirected P:System.Diagnostics.Process.StandardOutput stream of the ipconfig command.

The example creates an event delegate for the OutputHandler event handler and associates it with the E:System.Diagnostics.Process.OutputDataReceived event. The event handler receives text lines from the redirected P:System.Diagnostics.Process.StandardOutput stream, formats the text, and saves it in an output string that’s later shown in the example’s console window.

using System;
using System.IO;
using System.Diagnostics;
using System.Text;

class StandardAsyncOutputExample
{
    private static int lineCount = 0;
    private static StringBuilder output = new StringBuilder();

    public static void Main()
    {
        Process process = new Process();
        process.StartInfo.FileName = "ipconfig.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            // Prepend line numbers to each line of the output.
            if (!String.IsNullOrEmpty(e.Data))
            {
                lineCount++;
                output.Append("\n[" + lineCount + "]: " + e.Data);
            }
        });

        process.Start();

        // Asynchronously read the standard output of the spawned process. 
        // This raises OutputDataReceived events for each line of output.
        process.BeginOutputReadLine();
        process.WaitForExit();

        // Write the redirected output to this application's window.
        Console.WriteLine(output);

        process.WaitForExit();
        process.Close();

        Console.WriteLine("\n\nPress any key to exit.");
        Console.ReadLine();
    }
}

LinkDemand

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

.NET Framework
Disponible desde 2.0
Volver al principio
Mostrar: