<p>I have a System.Diagnostics.Process object in a program targeted at the .Net framework 3.5</p>

<p>I have redirected both <code>StandardOutput</code> and <code>StandardError</code> pipes and I'm receiving data from them asynchronously.  I've also set an event handler for the Exited event.</p>

<p>Once I call <code>Process.Start()</code> I want to go off and do other work whilst I wait for events to be raised.</p>

<p>Unfortunately it appears that, for a process which returns a large amount of information, the Exited event is fired before the last <code>OutputDataReceived</code> event.</p>

<p>How do I know when the last <code>OutputDataReceived</code> has been received?  Ideally I would like the <code>Exited</code> event to be the last event I receive.</p>

<p>Here is an example program:</p>

<pre><code>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);
    }


  }
}
</code></pre>

<p>When running this program you will notice that "Exited (Event)" appears in a completely variable location within the output.  You may need to run it a few times and, obviously, you will need to replace "output.exe" with a program of your choice that produces a suitably large amount of output.</p>

<p>So, the question again: How do I know when the last <code>OutputDataReceived</code> has been received? Ideally I would like the <code>Exited</code> event to be the last event I receive.</p>

[{'answer_id': 1423665, 'author': 'csharptest.net', 'author_id': 164392, 'author_profile': 'https://Stackoverflow.com/users/164392', 'pm_score': 5, 'selected': False, 'text': '<p>The answer to this is that <a href="https://msdn.microsoft.com/en-us/library/system.diagnostics.datareceivedeventhandler%28v=vs.110%29.aspx" rel="noreferrer"><code>e.Data</code> will be set to <code>null</code></a>:</p>\n\n<pre><code>static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)\n{\n     if( e.Data == null ) _exited.Set();\n}\n</code></pre>\n'}, {'answer_id': 63083735, 'author': 'user2763329', 'author_id': 2763329, 'author_profile': 'https://Stackoverflow.com/users/2763329', 'pm_score': 1, 'selected': False, 'text': '<p>It will be more comfortable if e.Data set to null, but actually, the value will be an empty string. Please note the first value also could be Empty string. The real answer is once you receive some value other than Empty string, then look for next Empty string. I am using Visual Studio 2019.</p>\n'}]
['https://Stackoverflow.com/questions/63303', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']