Process.OutputDataReceived Événement
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Se produit chaque fois qu’une application écrit une ligne dans son flux de StandardOutput redirigé.
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
Type d'événement
- Attributs
Exemples
L’exemple suivant montre comment effectuer des opérations de lecture asynchrones sur le flux redirigé StandardOutput de la ipconfig commande.
L’exemple crée un délégué d’événement pour le OutputHandler gestionnaire d’événements et l’associe à l’événement OutputDataReceived . Le gestionnaire d’événements reçoit des lignes de texte du flux redirigé StandardOutput , met en forme le texte et l’enregistre dans une chaîne de sortie qui s’affiche ultérieurement dans la fenêtre de console de l’exemple.
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
Remarques
L’événement OutputDataReceived indique que l’associé Process a écrit une ligne qui s’est arrêtée avec une nouvelle ligne (retour chariot (CR), un flux de ligne (LF) ou CR+LF) dans son flux redirigé StandardOutput .
L’événement est activé pendant les opérations de lecture asynchrones sur StandardOutput. Pour démarrer des opérations de lecture asynchrones, vous devez rediriger le StandardOutput flux d’un Process, ajouter votre gestionnaire d’événements à l’événement OutputDataReceived et appeler BeginOutputReadLine. Par la suite, l’événement OutputDataReceived signale chaque fois que le processus écrit une ligne dans le flux redirigé StandardOutput , jusqu’à ce que le processus quitte ou appelle CancelOutputRead.
Note
L’application qui traite la sortie asynchrone doit appeler la WaitForExit méthode pour s’assurer que la mémoire tampon de sortie a été vidée.