Nyelv

Process.OutputDataReceived Esemény

Definíció

Minden alkalommal előfordul, amikor egy alkalmazás sorokat ír az átirányított StandardOutput streambe.

public:
 event System::Diagnostics::DataReceivedEventHandler ^ OutputDataReceived;
public event System.Diagnostics.DataReceivedEventHandler? OutputDataReceived;
public event System.Diagnostics.DataReceivedEventHandler OutputDataReceived;
[System.ComponentModel.Browsable(true)]
public event System.Diagnostics.DataReceivedEventHandler OutputDataReceived;
member this.OutputDataReceived : System.Diagnostics.DataReceivedEventHandler 
[<System.ComponentModel.Browsable(true)>]
member this.OutputDataReceived : System.Diagnostics.DataReceivedEventHandler 
Public Custom Event OutputDataReceived As DataReceivedEventHandler 
Public Event OutputDataReceived As DataReceivedEventHandler 

Eseménytípus

Attribútumok

Példák

Az alábbi példa bemutatja, hogyan hajthat végre aszinkron olvasási műveleteket a StandardOutput parancs átirányított ipconfig adatfolyamán.

A példa létrehoz egy eseménydelegáltat az OutputHandler eseménykezelő számára, és társítja azt az OutputDataReceived eseményhez. Az eseménykezelő szövegsorokat fogad az átirányított StandardOutput streamből, formázza a szöveget, és egy kimeneti sztringbe menti, amely később megjelenik a példa konzolablakában.

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();
    }
}
open System
open System.Diagnostics
open System.Text

let mutable lineCount = 0
let output = StringBuilder()

let proc = new Process()
proc.StartInfo.FileName <- "ipconfig.exe"
proc.StartInfo.UseShellExecute <- false
proc.StartInfo.RedirectStandardOutput <- true

proc.OutputDataReceived.AddHandler(
    DataReceivedEventHandler(fun _ e ->
        // Prepend line numbers to each line of the output.
        if not (String.IsNullOrEmpty e.Data) then
            lineCount <- lineCount + 1
            output.Append $"\n[{lineCount}]: {e.Data}" |> ignore)
)


proc.Start() |> ignore

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

// Write the redirected output to this application's window.
printfn $"{output}"

proc.WaitForExit()
proc.Close()

printfn "\n\nPress any key to exit."
stdin.ReadLine() |> ignore
Imports System.IO
Imports System.Diagnostics
Imports System.Text

Module Module1
    Dim lineCount As Integer = 0
    Dim output As StringBuilder = New StringBuilder()

    Sub Main()
        Dim process As New Process()
        process.StartInfo.FileName = "ipconfig.exe"
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardOutput = True
        AddHandler process.OutputDataReceived, AddressOf OutputHandler
        process.Start()

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

        Console.WriteLine(output)

        process.WaitForExit()
        process.Close()

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

    Sub OutputHandler(sender As Object, e As DataReceivedEventArgs)
        If Not String.IsNullOrEmpty(e.Data) Then
            lineCount += 1

            ' Add the text to the collected output.
            output.Append(Environment.NewLine + "[" + lineCount.ToString() + "]: " + e.Data)
        End If
    End Sub
End Module

Megjegyzések

Az OutputDataReceived esemény azt jelzi, hogy a társított Process egy új vonallal (kocsivissza(CR), vonalcsatorna (LF) vagy CR+LF) végződő sort írt az átirányított StandardOutput adatfolyamba.

Az esemény akkor is bekövetkezik, ha az átirányított stream vége el van érve. Ebben az esetben az Data eseménykezelő tulajdonságaDataReceivedEventArgs.null

Az esemény engedélyezve van aszinkron olvasási műveletek során a következőn StandardOutput: . Az aszinkron olvasási műveletek elindításához át kell irányítania egy StandardOutput esemény adatfolyamát Process, hozzá kell adnia az eseménykezelőt az OutputDataReceived eseményhez, és fel kell hívnia BeginOutputReadLine. Ezt követően az OutputDataReceived esemény minden alkalommal jelez, amikor a folyamat egy sort ír az átirányított StandardOutput streamre, amíg a folyamat nem lép ki vagy nem hív CancelOutputRead.

Note

Az aszinkron kimenetet feldolgozó alkalmazásnak meg kell hívnia a WaitForExit metódust, hogy a kimeneti puffer kiürítve legyen.

A következőre érvényes:

Lásd még