title: Process.WaitForExit doesn't return even though Process.HasExited is true
body: <p>I use Process.Start to start a batch file. The batch file uses the "START" command to start several programs in parallel and then exits.</p>
  
  <p>Once the batch file is done Process.HasExited becomes true and Process.ExitCode contains the correct exit code.</p>
  
  <p>But when I call Process.WaitForExit() it hangs / never returns.</p>
  
  <p>The following piece of code demonstrates the problem. It creates a batch file, starts it and then prints:</p>
  
  <pre><code>Process is still running...
  Batch file is done!
  Process has exited. Exit code: 123
  Calling WaitForExit()...
  </code></pre>
  
  <p>It should then print:</p>
  
  <pre><code>WaitForExit returned.
  </code></pre>
  
  <p>... but it never does (even though HasExited is true and we already have an ExitCode).</p>
  
  <pre class="lang-ml prettyprint-override"><code>open System.IO
  open System.Diagnostics
  open System.Threading
  
  let foobat = """
    START ping -t localhost
    START ping -t google.com
    ECHO Batch file is done!
    EXIT /B 123
  """
  
  File.WriteAllText("foo.bat", foobat)
  
  use p = new Process(StartInfo = ProcessStartInfo("foo.bat",
                                                   UseShellExecute = false,
                                                   RedirectStandardOutput = true,
                                                   RedirectStandardError = true))
  
  let onOutput = DataReceivedEventHandler(fun _ args -&gt; printfn "%s" args.Data)
  
  p.OutputDataReceived.AddHandler onOutput
  p.ErrorDataReceived.AddHandler onOutput
  
  p.Start() |&gt; ignore
  
  p.BeginErrorReadLine()
  p.BeginOutputReadLine()
  
  while not p.HasExited do
    printfn "Process is still running..."
    Thread.Sleep(1000)
  
  printfn "Process has exited. Exit code: %d" p.ExitCode
  
  printfn "Calling WaitForExit()..."
  p.WaitForExit()|&gt; ignore
  printfn "WaitForExit returned."
  </code></pre>
  
  <p>I noticed that this only happens when the batch file contains "START" commands and when standard output and/or standard error are redirected.</p>
  
  <p>Why does WaitForExit() never return?</p>
  
  <p>What's the right way to wait for such a process to exit?</p>
  
  <p>Is it safe to just poll Process.HasExited or can that result in other problems?</p>
  
  <p>PS.: I just noticed that calling WaitForExit(<strong>100000</strong>) with a <strong>huge</strong> timeout (that definitely doesn't expire) returns immediately when the process exits. Wierd. Without timeout it hangs.</p>