title: If 'Process.HasExited' throws an exception, can I assume that the process is gone?
body: <p>I have the following code section, designed to count how many Excel processes are currently open:</p>
  
  <pre><code>Func&lt;int&gt; OpenExcelProcessesCount = 
      () =&gt; System.Diagnostics.Process.GetProcessesByName("Excel")
                .Where(p =&gt; !p.HasExited)
                .Count();
  </code></pre>
  
  <p>And then later I retrieve the count at various points, with code such as the following:</p>
  
  <pre><code>int excelAppCount = OpenExcelProcessesCount();
  </code></pre>
  
  <p>This code has been running 100% fine for months. Then suddenly, today, it is consistently giving me an exception that reads the following:</p>
  
  <blockquote>
  <pre><code>Exception: ApplicationThreadException
  
  Message: Access is denied
  
  Stack Trace:
  
     at System.Diagnostics.ProcessManager.OpenProcess(Int32
  </code></pre>
    
    <p>processId, Int32 access, Boolean
    throwIfExited)</p>
  
  <pre><code>   at System.Diagnostics.Process.GetProcessHandle(Int32
  </code></pre>
    
    <p>access, Boolean throwIfExited)</p>
  
  <pre><code>   at System.Diagnostics.Process.get_HasExited()
  
     etc...
  </code></pre>
  </blockquote>
  
  <p>Basically, the call to <code>Process.HasExited</code> (which shows up as <code>System.Diagnostics.Process.get_HasExited()</code> in the stack trace, above) is failing. The error message "Access is denied" sounds like I don't have administrative privileges for the process, but the only Excel processes that exist would be created under my current user log-in, and the user can always access their own processes. My .NET code is also running under full trust.</p>
  
  <p>The line that is ultimately failing is <code>System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)</code>. I wonder if it is being passed in a value of 'true' for the 'throwIfExited' parameter. If this is the case, then I suppose that I could protect the call to <code>Process.HasExited</code> with a try-catch block and assume that if this fails that <code>HasExited</code> is, in fact, 'true'. But is this a safe assumption??</p>
  
  <p>I am uneasy making a presumption like this, especially since the error message is "Access is denied." Does anyone have any ideas on how I might address this, or what I might test in an attempt to figure out what is going on?</p>
  
  <p>The only similar thread I could find on Stack Overflow was the following: <a href="https://stackoverflow.com/questions/774730/why-did-hasexited-throw-system-componentmodel-win32exception">Why did hasExited throw ‘System.ComponentModel.Win32Exception’?</a>. The answer given there was:</p>
  
  <blockquote>
    <p>"Since you are doing runas, you only
    get SYNCHRONIZE access on the handle,
    not PROCESS_QUERY_INFORMATION access,
    hence GetExitCodeProcess fails, which
    results in hasEnded throwing a Win32
    exception."</p>
  </blockquote>
  
  <p>I don't really understand this answer and do not know if this applies in my case, but I thought I should mention it. If anyone feels that it is likely that this is the situation that I am facing, then if someone could try to clarify this answer for me, I would greatly appreciate it. (I'm an Excel programmer, I do not have much experience working with processes.)</p>
  
  <p>Much thanks in advance...</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Best I can tell, this is was a one-off corruption of some sort. The issues I faced started becoming increasingly bizarre as what had been a perfectly functioning set of unit tests was starting to have breakdowns at other "impossible" locations. A simple reboot corrected this issue and everything else I was facing.</p>
  
  <p>My best guess is that I had some sort of bizarre corruption. Perhaps the ROT was corupted, and/or I had a hanging instance of Excel that was so corrupt that even 'Process' operations were not necessarily stable. Nothing conclusive, but this is all I can figure for now.</p>
  
  <p>To the responders who took the time answer and help me out, I thank you.</p>