Process.OutputDataReceived Gebeurtenis
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Vindt elke keer plaats wanneer een toepassing een regel naar de omgeleide StandardOutput stream schrijft.
public:
event System::Diagnostics::DataReceivedEventHandler ^ OutputDataReceived;
[System.ComponentModel.Browsable(true)]
public event System.Diagnostics.DataReceivedEventHandler OutputDataReceived;
[<System.ComponentModel.Browsable(true)>]
member this.OutputDataReceived : System.Diagnostics.DataReceivedEventHandler
Public Custom Event OutputDataReceived As DataReceivedEventHandler
Public Event OutputDataReceived As DataReceivedEventHandler
Gebeurtenistype
- Kenmerken
Voorbeelden
In het volgende voorbeeld ziet u hoe u asynchrone leesbewerkingen uitvoert op de omgeleide StandardOutput stroom van de ipconfig opdracht.
In het voorbeeld wordt een gebeurtenisdelegen voor de OutputHandler gebeurtenis-handler gemaakt en gekoppeld aan de OutputDataReceived gebeurtenis. De gebeurtenishandler ontvangt tekstregels van de omgeleide StandardOutput stroom, maakt de tekst op en slaat deze op in een uitvoertekenreeks die later wordt weergegeven in het consolevenster van het voorbeeld.
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
Opmerkingen
De OutputDataReceived gebeurtenis geeft aan dat de bijbehorende Process regel een regel heeft geschreven die is beƫindigd met een nieuwe regel (regelterugloop (CR), regelfeed (LF) of CR+LF) naar de omgeleide StandardOutput stream.
De gebeurtenis is ingeschakeld tijdens asynchrone leesbewerkingen op StandardOutput. Als u asynchrone leesbewerkingen wilt starten, moet u de StandardOutput stroom van een Process, uw gebeurtenis-handler toevoegen aan de OutputDataReceived gebeurtenis en aanroepen BeginOutputReadLine. Daarna wordt de OutputDataReceived gebeurtenis signalen telkens wanneer het proces een regel naar de omgeleide StandardOutput stream schrijft, totdat het proces wordt afgesloten of aanroepen CancelOutputRead.
Note
De toepassing die de asynchrone uitvoer verwerkt, moet de WaitForExit methode aanroepen om ervoor te zorgen dat de uitvoerbuffer is leeggemaakt.