private void CloseOrDisconnectAllRemoteRunspaces(Func <List <RemoteRunspace> > getRunspaces) { List <RemoteRunspace> list = getRunspaces(); if (list.Count != 0) { EventHandler <EventArgs> handler = null; using (ManualResetEvent remoteRunspaceCloseCompleted = new ManualResetEvent(false)) { ThrottleManager manager = new ThrottleManager(); if (handler == null) { handler = (sender, e) => remoteRunspaceCloseCompleted.Set(); } manager.ThrottleComplete += handler; foreach (RemoteRunspace runspace in list) { IThrottleOperation operation = new CloseOrDisconnectRunspaceOperationHelper(runspace); manager.AddOperation(operation); } manager.EndSubmitOperations(); remoteRunspaceCloseCompleted.WaitOne(); } } }
private void SubmitAndWaitForConnect(List <IThrottleOperation> connectJobOperations) { using (ThrottleManager manager = new ThrottleManager()) { EventHandler <EventArgs> handler2 = null; using (ManualResetEvent connectResult = new ManualResetEvent(false)) { if (handler2 == null) { handler2 = (sender, eventArgs) => connectResult.Set(); } EventHandler <EventArgs> handler = handler2; manager.ThrottleComplete += handler; try { manager.ThrottleLimit = 0; manager.SubmitOperations(connectJobOperations); manager.EndSubmitOperations(); connectResult.WaitOne(); } finally { manager.ThrottleComplete -= handler; } } } }
private void StopOrDisconnectAllJobs() { List <RemoteRunspace> disconnectRunspaces; if (this.JobRepository.Jobs.Count != 0) { disconnectRunspaces = new List <RemoteRunspace>(); EventHandler <EventArgs> handler = null; using (ManualResetEvent jobsStopCompleted = new ManualResetEvent(false)) { ThrottleManager manager = new ThrottleManager(); if (handler == null) { handler = (sender, e) => jobsStopCompleted.Set(); } manager.ThrottleComplete += handler; foreach (Job job in this.JobRepository.Jobs) { if (job is PSRemotingJob) { if (!job.CanDisconnect) { manager.AddOperation(new StopJobOperationHelper(job)); } else if (job.JobStateInfo.State == JobState.Running) { IEnumerable <RemoteRunspace> runspaces = job.GetRunspaces(); if (runspaces != null) { disconnectRunspaces.AddRange(runspaces); } } } } manager.EndSubmitOperations(); jobsStopCompleted.WaitOne(); } this.CloseOrDisconnectAllRemoteRunspaces(() => disconnectRunspaces); } }
internal PSInvokeExpressionSyncJob( List <IThrottleOperation> operations, ThrottleManager throttleManager) { this.Results.AddRef(); this.throttleManager = throttleManager; this.RegisterThrottleComplete(this.throttleManager); foreach (IThrottleOperation operation in operations) { ExecutionCmdletHelper helper = operation as ExecutionCmdletHelper; if (helper.Pipeline.Runspace is RemoteRunspace runspace && runspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen) { runspace.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(((PSRemotingChildJob)this).HandleURIDirectionReported); runspace.StateChanged += new EventHandler <RunspaceStateEventArgs>(this.HandleRunspaceStateChanged); } this.helpers.Add(helper); this.AggregateResultsFromHelper(helper); RemotePipeline pipeline = helper.Pipeline as RemotePipeline; this.powershells.Add(pipeline.PowerShell.InstanceId, pipeline.PowerShell); } throttleManager.SubmitOperations(operations); throttleManager.EndSubmitOperations(); }