protected virtual void Dispose(bool disposing)
        {
            if (_isDisposed)
            {
                return;
            }

            if (disposing)
            {
                if (IndisposableStates.HasFlag(PowerShell.InvocationStateInfo.State))
                {
                    PowerShell.BeginStop(
                        asyncResult =>
                    {
                        PowerShell.Runspace = null;
                        PowerShell.Dispose();
                    },
                        state: null);
                }
                else
                {
                    PowerShell.Runspace = null;
                    PowerShell.Dispose();
                }

                _frameExited.Release();
            }

            _isDisposed = true;
        }
        public async static Task StopAsync(this PowerShell source)
        {
            var asyncResult = source.BeginStop(null, null);

            var factory = new TaskFactory();

            await factory.FromAsync(asyncResult, x => x);
        }
示例#3
0
 public void Stop()
 {
     if (_powerShell.InvocationStateInfo.State != PSInvocationState.Stopped)
     {
         UnhookStreamEvents(_powerShell.Streams);
         _powerShell.BeginStop(OnStopped, null);
     }
 }
示例#4
0
 //! This method is sync and uses pipeline, that is why we must not null the pipeline async.
 void OnCtrlCPressed(object sender, EventArgs e)
 {
     if (PowerShell != null && PowerShell.InvocationStateInfo.State == PSInvocationState.Running)
     {
         //! Stop() tends to hang.
         PowerShell.BeginStop(AsyncStop, null);
     }
 }
示例#5
0
 private void Execute_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (m_PSInst.InvocationStateInfo.State == PSInvocationState.Running || m_PSInst.InvocationStateInfo.State == PSInvocationState.Stopping)
     {
         e.Cancel = true;
         AddProgress(null, new ProgressEventArgs {
             Message = "Stopping...this might take a second", Percent = 0
         });
         m_PSInst.BeginStop(StopGracefully, null);
     }
 }
        internal void CancelRequest()
        {
            if (!_reentrancyLock.WaitOne(0))
            {
                // it's running right now.
#if DEBUG
                NativeMethods.OutputDebugString("[Cmdlet:debugging] -- Stopping powershell script.");
#endif
                lock (_stopLock) {
                    if (_stopResult == null)
                    {
                        _stopResult = _powershell.BeginStop(ar => { }, null);
                    }
                }
            }
        }
示例#7
0
        /// <summary>
        /// Stops the job.
        /// </summary>
        public void StopJob()
        {
            // _091006_191117, _091006_191214
            try
            {
                PowerShell.BeginStop(PowerShell.EndStop, null);
                while (!IsFinished)
                {
                    Thread.Sleep(50);
                }
            }
            catch (PSObjectDisposedException)
            { }

            if (JobUI != null)
            {
                JobUI.HasError = true;
            }
        }
示例#8
0
        /// <summary>
        /// Asyncronously executes the PowerShell commands.
        /// </summary>
        /// <param name="powerShell">The <seealso cref="PowerShell"/> object.</param>
        /// <param name="cancelToken">When cancelation is requested, this method stops the execution and throws.</param>
        /// <returns>
        /// The <see cref="PSDataCollection{T}"/> returned by the powershell execution.
        /// </returns>
        public static async Task <PSDataCollection <PSObject> > InvokeAsync(
            this PowerShell powerShell,
            CancellationToken cancelToken = default(CancellationToken))
        {
            cancelToken.ThrowIfCancellationRequested();

            powerShell.InvocationStateChanged += (sender, args) =>
            {
                if (args.InvocationStateInfo.State == PSInvocationState.Running)
                {
                    cancelToken.Register(() => powerShell.BeginStop(null, null));
                }
            };

            try
            {
                return(await Task.Factory.FromAsync(powerShell.BeginInvoke(), powerShell.EndInvoke));
            }
            finally
            {
                cancelToken.ThrowIfCancellationRequested();
            }
        }
示例#9
0
 /// <summary>
 /// StopJobAsync
 /// </summary>
 public override void StopJobAsync()
 {
     _ps.BeginStop((iasync) => { OnStopJobCompleted(new AsyncCompletedEventArgs(null, false, this)); }, null);
 }
示例#10
0
 private void Button_Click_3(object sender, RoutedEventArgs e)
 {
     _poshInstance.BeginStop(null, null);
 }