title: Why does StandardOutput.Read() block when StartInfo.RedirectStandardInput is set to true?
body: <p>I'm having a hard time deciphering the MSDN doc about <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx" rel="nofollow">Process.StandardOutpout</a> as to if the Read(Char[], Int32, Int32) method blocks or not. My understanding is that it shouldn't block, but it seems like it does when I set RedirectStandardInput to true.</p>
  
  <p>Does anybody have experience with this; or some explanation for the issue I'm having?</p>
  
  <p>The context here is that I don't want to wait for a full line (ie with a line terminator), or for the process to exit before reading standard output. Also I don't want to use callbacks. <strong>I want to read StdOut synchronously as the process writes to it.</strong></p>
  
  <p>Here is a simplified version of my code:</p>
  
  <pre><code>string command = @"C:\flex_sdks\flex_sdk_4.5.1.21328\bin\fcsh.exe";
  Process p = new Process();
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.RedirectStandardInput = false; # &lt;-- if I set this to true, then 
                                             # the program hangs on
                                             # p.StandardOutput.Read later on
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.CreateNoWindow = true;
  p.StartInfo.FileName = command;
  p.Start();
  
  StringBuilder sb_stdout = new StringBuilder(1024);
  char[] buffer = new char[64];
  int nb_bytes_read;
  while (true) {
      do {
          nb_bytes_read = p.StandardOutput.Read(buffer, 0, buffer.Length);
          sb_stdout.Append(new string(buffer, 0, nb_bytes_read));
      } while (nb_bytes_read &gt; 0);
      if (sb_stdout.ToString().EndsWith("\n(fcsh) "))
          break;
      Thread.Sleep(20);
  }
  </code></pre>
  
  <h2>Update</h2>
  
  <p>Based on my (probably bad) assumption that Process.StandardOutput is broken when used:</p>
  
  <ul>
  <li>with stdin redirected; and,</li>
  <li>while reading something else than terminated lines from stdout or stderr,</li>
  </ul>
  
  <p>I decided to try using Windows' API directly. I added an answer with such code; it works fine (at least for now).</p>
  
  <h2>Another update</h2>
  
  <p>I created a <a href="http://sixfeetsix.blogspot.ch/2012/08/interacting-with-sub-processed-shell-in.html" rel="nofollow">blog entry</a> with the code I now use.</p>