Example #1
0
        private static Task <IList <PSObject> > InvokeAsync(this PowerShell ps, CancellationToken cancellationToken)
        {
            CancellationTokenRegistration ctr;

            if (cancellationToken.CanBeCanceled)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(cancellationToken.AsCanceledTask <IList <PSObject> >());
                }

                ctr = cancellationToken.Register(p => { ((PowerShell)p).BeginStop((ar) => { }, null); }, ps);
            }
            else
            {
                ctr = new CancellationTokenRegistration();
            }

            var taskFactory = new TaskFactory <IList <PSObject> >(cancellationToken);
            var task        = taskFactory.FromAsync((callback, state) => ps.BeginInvoke <object>(null, null, callback, state), ps.EndInvoke, null);

            task.ContinueWith((antecedent) => { try { ctr.Dispose(); } catch { } });

            return(task);
        }
        /// <summary>
        /// Starts the associated process with a cancellation token as an asynchronous operation.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the process.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public Task <int> ExecuteAsync(CancellationToken cancellationToken)
        {
            if (_started)
            {
                throw new InvalidOperationException();
            }

            _workingTaskCompletionSource = new TaskCompletionSource <int>();
            CancellationTokenRegistration ctr;

            if (cancellationToken.CanBeCanceled)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(cancellationToken.AsCanceledTask <int>(_workingTaskCompletionSource));
                }

                ctr = cancellationToken.Register(() =>
                {
                    lock (_process)
                    {
                        if (_started)
                        {
                            _process.Kill();
                        }
                        _canceled = true;
                    }
                });
            }
            else
            {
                ctr = new CancellationTokenRegistration();
            }

            if (!_process.EnableRaisingEvents)
            {
                _process.EnableRaisingEvents = true;
                _process.Exited += OnProcess_Exited;
            }

            _workingTaskCompletionSource.Task.ContinueWith((antecedent) => { try { ctr.Dispose(); } catch { } });

            lock (_process)
            {
                if (_canceled)
                {
                    _workingTaskCompletionSource.TrySetCanceled(/*cancellationToken*/);
                }
                else
                {
                    StartProcess();
                }
            }

            return(_workingTaskCompletionSource.Task);
        }