Example #1
0
        public static WaitAsyncOperation Start(WaitHandle waitHandle, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var value = new WaitAsyncOperation();

            value.StartImpl(waitHandle, timeout, cancellationToken);
            return(value);
        }
Example #2
0
        public Task <bool> WaitForExitAsync(TimeSpan timeout, CancellationToken cancellationToken = default)
        {
            ValidateTimeoutRange(timeout);
            CheckNotDisposed();

            var state = _stateHolder.State;

            if (state.HasExitCode)
            {
                return(CompletedBoolTask.True);
            }

            // Synchronous path: the process has already exited.
            var waitHandle = _stateHolder.State.ExitedWaitHandle;

            if (waitHandle.WaitOne(0))
            {
                state.DangerousRetrieveExitCode();
                return(CompletedBoolTask.True);
            }

            // Synchronous path: already canceled.
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled <bool>(cancellationToken));
            }

            // Start an asynchronous wait operation.
            var operation = WaitAsyncOperation.Start(waitHandle, timeout, cancellationToken);

            return(operation.Completion);
        }