title: Capture output of process synchronously (i.e. "when it happens")
body: <p>I am trying to start a process and capture the output, have come a far way, but am not quite at the solution I'd want.</p>
  
  <p>Specifically, I am trying to reset the IIS on my development machine from a small utility application that I am writing. I have come to the conclusion, by experimenting, that the safe way to do this is by running <code>iisreset.exe</code> in a child process.</p>
  
  <p>If you run <code>iisreset.exe</code> on a command prompt, you get feedback during the process. Running iisreset takes several seconds, and several lines of feedback is generated, with pauses in between. </p>
  
  <p>I'd like to capture this feedback and present it in my Windows Forms application (in a <code>ListBox</code>), and I have succeeded with that. My remaining concern is that I dont get it until the child process finishes. I'd like to get the output from the child process, line by line, immediately when the lines are created. </p>
  
  <p>I have tried to do my homework, reading/testing things from e.g. these:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net">How to spawn a process and capture its STDOUT in .NET?</a></li>
  <li><a href="https://stackoverflow.com/questions/186822/capturing-the-console-output-in-net-c">Capturing console output from a .NET application (C#)</a></li>
  <li><a href="http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx" rel="noreferrer">http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx</a></li>
  </ul>
  
  <p>and several more with similar content. Most (all?) get the output asynchronously (e.g. with <code>Process.ReadToEnd()</code>). I want the output synchonously, which acording to the MSDN documentation involves establishing an event handler etc and I've tried that. It works, but the event handler does not get called until the process exits. I get the output from iisreset.exe, but not until it has finished.</p>
  
  <p>To rule out the possibility that this has something to do with iisreset.exe in particular, I wrote a small console application that generates some output, pausing in between:</p>
  
  <pre><code>namespace OutputGenerator
  {
      class Program
      {
          static void Main(string[] args)
          {
              System.Console.WriteLine("OutputGenerator starting and pausing for 10 seconds..");
              System.Threading.Thread.Sleep(10000);
              System.Console.WriteLine("Pausing for another 10 seconds..");
              System.Threading.Thread.Sleep(10000);
              System.Console.WriteLine("Exiting!");
          }
      }
  }
  </code></pre>
  
  <p>Testing with this it turns out that I get captured data diretly when I want. So, to some extent it seems that the way iisreset.exe outputs the data come into play here.</p>
  
  <p>Here is the code of the program (a Windows Forms application) that does the capture:</p>
  
  <pre><code>using System;
  using System.Windows.Forms;
  using System.Diagnostics; 
  
  namespace OutputCapturer
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void btnRun_Click(object sender, EventArgs e)
          {
              // Running this will show all output after the process has exited
              //String path = @"C:\Windows\system32\iisreset.exe";
  
              // Running this will show all output "when it happens"
              String path = @"C:\OutputGenerator.exe";
  
              var p = new Process();
              p.StartInfo.FileName = path;
              p.StartInfo.UseShellExecute = false;  // ShellExecute = true not allowed when output is redirected..
              p.StartInfo.RedirectStandardOutput = true;
              p.StartInfo.CreateNoWindow = true;
              p.OutputDataReceived += OutputDataReceived;
              p.Start();
              p.BeginOutputReadLine();
          }
  
          private delegate void OutputDataToTextboxDelegate(String s);
  
          void OutputDataToTextbox(String s)
          {
              tbxOutput.Text += s + Environment.NewLine;
              tbxOutput.Refresh();
          }
  
          private void OutputDataReceived(object sender, DataReceivedEventArgs e)
          {
              if (e.Data != null &amp;&amp; e.Data.ToString() != "")
              {
                  // Must run the update of the textbox in the same thread that created it..
                  tbxOutput.Invoke(
                      new OutputDataToTextboxDelegate(OutputDataToTextbox), 
                      DateTime.Now.ToString() + ": " + e.Data.ToString()
                  );
              }
          }
      }
  }
  </code></pre>
  
  <p>Thinking it was an EOL-encoding problem (the output of <code>iisreset.exe</code> apearing as one line to my app)), I ran a debug session. Nope. The event handler for <code>StandardOutput</code> gets called several times (one time for each output line from <code>iisreset.exe</code>), buth these calls come in one burst after the process exits.</p>
  
  <p>I would LOVE if I could get the output from <code>iisreset.exe</code> "when it happens" so that I can show it as a progress indication.</p>
  
  <p>I've seen one other thread with the same/similar problem,  <a href="https://stackoverflow.com/questions/3626537/asynchronous-capture-from-a-process-output-not-working-properly">Asynchronous capture from a process output not working properly</a> , but w/o a solution.</p>
  
  <p>I'm sort of stumped.</p>