title: Process.HasExited returns true even though process is running?
body: <p>I have been observing that <code>Process.HasExited</code> sometimes returns <code>true</code> even though the process is still running.</p>
  
  <p>My code below starts a process with name "testprogram.exe" and then waits for it to exit. The problem is that sometimes I get thrown the exception; it seems that even though <code>HasExited</code> returns <code>true</code> the process itself is still alive in the system - how can this be??</p>
  
  <p>My program writes to a log file just before it terminates and thus I need to be absolutely sure that this log file exists (aka the process has terminated/finished) before reading it. Continuously checking for it's existence is not an option.</p>
  
  <pre><code>// Create new process object
  process = new Process();
  
  // Setup event handlers
  process.EnableRaisingEvents = true;
  process.OutputDataReceived += OutputDataReceivedEvent;
  process.ErrorDataReceived += ErrorDataReceivedEvent;
  process.Exited += ProgramExitedEvent;
  
  // Setup start info
  ProcessStartInfo psi = new ProcessStartInfo
                             {
                                 FileName = ExePath,
                                 // Must be false to redirect IO
                                 UseShellExecute = false,
                                 RedirectStandardOutput = true,
                                 RedirectStandardError = true,
                                 Arguments = arguments
                             };
  
  process.StartInfo = psi;
  
  // Start the program
  process.Start();
  
  while (!process.HasExited)
      Thread.Sleep( 500 );
  
  Process[] p = Process.GetProcessesByName( "testprogram" );
  
  if ( p.Length != 0 )
      throw new Exception("Oh oh");
  </code></pre>
  
  <p>UPDATE: I just tried waiting with <code>process.WaitForExit()</code> instead of the polling loop and the result is the exact same.</p>
  
  <p>Addition: The above code was only to demonstrate a 'clearer' problem alike. To make it clear; my problem is NOT that I still can get a hold of the process by <code>Process.GetProcessesByName( "testprogram" );</code> after it set <code>HasExited</code> to true.</p>
  
  <p>The real problem is that the program I am running externally writes a file -just before- it terminates (gracefully). I use <code>HasExited</code> to check when the process has finished and thus I know I can read the file (because the process exited!), but it seems that <code>HasExited</code> returns <code>true</code> even sometimes when the program has NOT written the file to disk yet. Here's example code that illustrates the exact problem:</p>
  
  <pre><code>// Start the program
  process.Start();
  
  while (!process.HasExited)
      Thread.Sleep( 500 );
  // Could also be process.WaitForExit(), makes no difference to the result
  
  // Now the process has quit, I can read the file it has exported
  if ( !File.Exists( xmlFile ) )
  {
      // But this exception is thrown occasionally, why?
      throw new Exception("xml file not found");
  }
  </code></pre>