/// <summary> /// Very ungracefully kills the runspace. Use only in the most dire emergency. /// </summary> public void Kill() { if (Runspace != null) { try { Runspace.Runspace.Close(); } catch { } Runspace.Dispose(); Runspace = null; } _State = PsfRunspaceState.Stopped; }
/// <summary> /// Gracefully stops the Runspace /// </summary> public void Stop() { _State = PsfRunspaceState.Stopping; int i = 0; // Wait up to the limit for the running script to notice and kill itself while ((Runspace != null) && (Runspace.Runspace != null) && (Runspace.Runspace.RunspaceAvailability == RunspaceAvailability.Busy) && (i < (10 * RunspaceHost.StopTimeoutSeconds))) { i++; Thread.Sleep(100); } Kill(); }
/// <summary> /// Starts the Runspace. /// </summary> public void Start() { if ((Runspace != null) && (State == PsfRunspaceState.Stopped)) { Kill(); } if (Runspace == null) { Runspace = PowerShell.Create(); try { SetName(Runspace.Runspace); } catch { } Runspace.AddScript(Script.ToString()); _State = PsfRunspaceState.Running; try { Runspace.BeginInvoke(); } catch { _State = PsfRunspaceState.Stopped; } } }
/// <summary> /// Signals the registered runspace has stopped execution /// </summary> public void SignalStopped() { _State = PsfRunspaceState.Stopped; }