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); }
public void Stop() { if (_powerShell.InvocationStateInfo.State != PSInvocationState.Stopped) { UnhookStreamEvents(_powerShell.Streams); _powerShell.BeginStop(OnStopped, null); } }
//! 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); } }
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); } } } }
/// <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; } }
/// <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(); } }
/// <summary> /// StopJobAsync /// </summary> public override void StopJobAsync() { _ps.BeginStop((iasync) => { OnStopJobCompleted(new AsyncCompletedEventArgs(null, false, this)); }, null); }
private void Button_Click_3(object sender, RoutedEventArgs e) { _poshInstance.BeginStop(null, null); }