In my code I need to call Process.Kill() that is declared in MSDN to throw Win32Exception when either of the following occurs
- The associated process could not be terminated.
- The process is terminating.
- The associated process is a Win16 executable.
and sometimes I indeed face Win32Exception with Access is denied message and NativeErrorCode set to 5. MSDN says this combination happens when Kill() is called while the process is terminating. The other two cases are not documented in such details.
So I need to reasonably handle this situation. Of course I can just catch Win32Exception but how do I know why exactly it was thrown? I need to do nothing if the process is already terminating and perhaps rethrow the exception in all other cases.
How do I identify and handle this specific case?