public static bool HasExitedSafe([NotNull] this Process process) { if (process == null) { throw new ArgumentNullException(nameof(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); } }
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); } }
private static async Task WaitForExitAsyncFallback([NotNull] Process process, CancellationToken cancellationToken = default(CancellationToken)) { if (process == null) { throw new ArgumentNullException(nameof(process)); } var handle = Kernel32.OpenProcess(Kernel32.ProcessAccessFlags.QueryLimitedInformation | Kernel32.ProcessAccessFlags.Synchronize, false, process.Id); if (handle == IntPtr.Zero || handle == new IntPtr(-1)) { await WaitForExitAsyncDeeperFallback(process, cancellationToken); return; } try { int exitCode; if (Kernel32.GetExitCodeProcess(handle, out exitCode) && exitCode != Kernel32.STILL_ACTIVE) { return; } using (var w = new ProcessWrapper.ProcessWaitHandle(handle)) { AcToolsLogging.Write("Waiting using ProcessWaitHandle…"); while (!w.WaitOne(0, false)) { await Task.Delay(300, cancellationToken); if (cancellationToken.IsCancellationRequested) { return; } } } } finally { Kernel32.CloseHandle(handle); } }