Пример #1
0
            public static Task <bool> WaitForProcessExitAsync(uint pid)
            {
                void *hProcess = OpenProcess(SYNCHRONIZE, 0, pid);

                if (hProcess == null)
                {
                    return(Task.FromResult(false));
                }

                var tasker = new TaskCompletionSource <bool>();

                var hProcessSafe = new NativeWaitHandle(hProcess, true);
                RegisteredWaitHandle registered = null;
                WaitOrTimerCallback  λHappened  = (state, timeout) =>
                {
                    hProcessSafe.Close();
#pragma warning disable once AccessToModifiedClosure
                    registered?.Unregister(null);
                    tasker.SetResult(true);
                };

                registered = ThreadPool.RegisterWaitForSingleObject(hProcessSafe, λHappened, Missing.Value, -1, true);

                return(tasker.Task);
            }
Пример #2
0
            public static Task<bool> WaitForProcessExitAsync(uint pid)
            {
                void* hProcess = OpenProcess(SYNCHRONIZE, 0, pid);
                if(hProcess == null)
                    return TaskHelpers.FromResult(false);

                var tasker = new TaskCompletionSource<bool>();

                var hProcessSafe = new NativeWaitHandle(hProcess, true);
                RegisteredWaitHandle registered = null;
                WaitOrTimerCallback λHappened = (state, timeout) =>
                {
                    hProcessSafe.Close();
                #pragma warning disable once AccessToModifiedClosure
                    registered?.Unregister(null);
                    tasker.SetResult(true);
                };
                registered = ThreadPool.RegisterWaitForSingleObject(hProcessSafe, λHappened, Missing.Value, -1, true);

                return tasker.Task;
            }