public static bool HasExitedSafe(this Process process) {
var handle = Kernel32.OpenProcess(Kernel32.ProcessAccessFlags.QueryLimitedInformation | Kernel32.ProcessAccessFlags.Synchronize, false, process.Id);
if (handle == IntPtr.Zero || handle == new IntPtr(-1)) return true;
try {
int exitCode;
if (Kernel32.GetExitCodeProcess(handle, out exitCode) && exitCode != Kernel32.STILL_ACTIVE) return true;
using (var w = new ProcessWrapper.ProcessWaitHandle(handle)) {
return w.WaitOne(0, false);
}
} finally {
Kernel32.CloseHandle(handle);
}
}