Esempio n. 1
0
        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 {
                if (Kernel32.GetExitCodeProcess(handle, out var exitCode) && exitCode != Kernel32.STILL_ACTIVE)
                {
                    return(true);
                }
                using (var w = new ProcessWrapper.ProcessWaitHandle(handle)) {
                    return(w.WaitOne(0, false));
                }
            } finally {
                Kernel32.CloseHandle(handle);
            }
        }
Esempio n. 2
0
        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 {
                if (Kernel32.GetExitCodeProcess(handle, out var exitCode) && exitCode != Kernel32.STILL_ACTIVE)
                {
                    return;
                }
                using (var w = new ProcessWrapper.ProcessWaitHandle(handle)) {
                    TypoLogging.Write("Waiting using ProcessWaitHandle…");

                    while (!w.WaitOne(0, false))
                    {
                        await Task.Delay(300, cancellationToken);

                        if (cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                }
            } finally {
                Kernel32.CloseHandle(handle);
            }
        }