【问题标题】:How do I know when the last OutputDataReceived has arrived?我怎么知道最后一个 OutputDataReceived 何时到达?
【发布时间】:2010-09-08 23:07:33
【问题描述】:

我在一个针对 .Net framework 3.5 的程序中有一个 System.Diagnostics.Process 对象

我已经重定向了StandardOutputStandardError 管道,并且我正在异步接收来自它们的数据。我还为 Exited 事件设置了一个事件处理程序。

一旦我打电话给Process.Start(),我想在等待事件发生的同时离开并做其他工作。

不幸的是,对于返回大量信息的进程,Exited 事件在最后一个 OutputDataReceived 事件之前被触发。

我如何知道收到最后一个OutputDataReceived 的时间?理想情况下,我希望 Exited 事件成为我收到的最后一个事件。

这是一个示例程序:

using System;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
  class Program
  {

    static void Main(string[] args)
    {
      string command = "output.exe";
      string arguments = " whatever";

      ProcessStartInfo info = new ProcessStartInfo(command, arguments);

      // Redirect the standard output of the process. 
      info.RedirectStandardOutput = true;
      info.RedirectStandardError = true;

      // Set UseShellExecute to false for redirection
      info.UseShellExecute = false;

      Process proc = new Process();
      proc.StartInfo = info;
      proc.EnableRaisingEvents = true;

      // Set our event handler to asynchronously read the sort output.
      proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
      proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived);
      proc.Exited += new EventHandler(proc_Exited);

      proc.Start();
      // Start the asynchronous read of the sort output stream. Note this line!
      proc.BeginOutputReadLine();
      proc.BeginErrorReadLine();

      proc.WaitForExit();

      Console.WriteLine("Exited (Main)");

    }

    static void proc_Exited(object sender, EventArgs e)
    {

      Console.WriteLine("Exited (Event)");
    }



    static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
      Console.WriteLine("Error: {0}", e.Data);
    }



    static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
      Console.WriteLine("Output data: {0}", e.Data);
    }


  }
}

运行此程序时,您会注意到“Exited (Event)”出现在输出中一个完全可变的位置。您可能需要运行它几次,显然,您需要将“output.exe”替换为您选择的程序,以产生适当的大量输出。

那么,问题又来了:我怎么知道最后一个OutputDataReceived 是什么时候收到的?理想情况下,我希望 Exited 事件成为我收到的最后一个事件。

【问题讨论】:

    标签: c# asynchronous system.diagnostics


    【解决方案1】:

    这个问题的答案是e.Data will be set to null:

    static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
         if( e.Data == null ) _exited.Set();
    }
    

    【讨论】:

      【解决方案2】:

      如果 e.Data 设置为 null 会更舒服,但实际上,该值将是一个空字符串。请注意,第一个值也可以是空字符串。真正的答案是,一旦您收到空字符串以外的其他值,然后查找下一个空字符串。我正在使用 Visual Studio 2019。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 2020-02-03
        • 2022-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多