Exemple #1
0
		internal PSWmiChildJob(Cmdlet cmds, string computerName, ThrottleManager throttleManager) : base(null, null)
		{
			this.syncObject = new object();
			this.statusMessage = "test";
			base.UsesResultsCollection = true;
			this.computerName = computerName;
			this.throttleManager = throttleManager;
			this.wmiSinkArray = new ArrayList();
			ManagementOperationObserver managementOperationObserver = new ManagementOperationObserver();
			this.wmiSinkArray.Add(managementOperationObserver);
			PSWmiChildJob pSWmiChildJob = this;
			pSWmiChildJob.sinkCompleted = pSWmiChildJob.sinkCompleted + 1;
			managementOperationObserver.ObjectReady += new ObjectReadyEventHandler(this.NewObject);
			managementOperationObserver.Completed += new CompletedEventHandler(this.JobDone);
			this.helper = new WmiAsyncCmdletHelper(this, cmds, computerName, managementOperationObserver);
			this.helper.WmiOperationState += new EventHandler<WmiJobStateEventArgs>(this.HandleWMIState);
			this.helper.ShutdownComplete += new EventHandler<EventArgs>(this.JobDoneForWin32Shutdown);
			base.SetJobState(JobState.NotStarted);
			IThrottleOperation throttleOperation = this.helper;
			throttleOperation.OperationComplete += new EventHandler<OperationStateEventArgs>(this.HandleOperationComplete);
			throttleManager.ThrottleComplete += new EventHandler<EventArgs>(this.HandleThrottleComplete);
			throttleManager.AddOperation(throttleOperation);
		}
 internal static void AddOperation(object operation, ThrottleManager throttleManager)
 {
     throttleManager.AddOperation((IThrottleOperation)operation);
 }
Exemple #3
0
 /// <summary>
 /// Internal constructor for initializing WMI jobs, where WMI command is executed a variable
 /// number of times.
 /// </summary>
 internal PSWmiChildJob(Cmdlet cmds, string computerName, ThrottleManager throttleManager, int count)
     : base(null, null)
 {
     UsesResultsCollection = true;
     Location = computerName;
     _throttleManager = throttleManager;
     _wmiSinkArray = new ArrayList();
     ManagementOperationObserver wmiSink = new ManagementOperationObserver();
     _wmiSinkArray.Add(wmiSink);
     _sinkCompleted += count;
     wmiSink.ObjectReady += new ObjectReadyEventHandler(this.NewObject);
     wmiSink.Completed += new CompletedEventHandler(this.JobDone);
     _helper = new WmiAsyncCmdletHelper(this, cmds, computerName, wmiSink, count);
     _helper.WmiOperationState += new EventHandler<WmiJobStateEventArgs>(HandleWMIState);
     _helper.ShutdownComplete += new EventHandler<EventArgs>(JobDoneForWin32Shutdown);
     SetJobState(JobState.NotStarted);
     IThrottleOperation operation = _helper;
     operation.OperationComplete += new EventHandler<OperationStateEventArgs>(HandleOperationComplete);
     throttleManager.ThrottleComplete += new EventHandler<EventArgs>(HandleThrottleComplete);
     throttleManager.AddOperation(operation);
 }
Exemple #4
0
        /// <summary>
        /// Disconnects all disconnectable jobs listed in the JobRepository.
        /// </summary>
        private void StopOrDisconnectAllJobs()
        {
            if (JobRepository.Jobs.Count == 0) { return; }
            List<RemoteRunspace> disconnectRunspaces = new List<RemoteRunspace>();

            using (ManualResetEvent jobsStopCompleted = new ManualResetEvent(false))
            {
                ThrottleManager throttleManager = new ThrottleManager();
                throttleManager.ThrottleComplete += delegate (object sender, EventArgs e)
                {
                    jobsStopCompleted.Set();
                };

                foreach (Job job in this.JobRepository.Jobs)
                {
                    // Only stop or disconnect PowerShell jobs.
                    if (job is PSRemotingJob == false)
                    {
                        continue;
                    }

                    if (!job.CanDisconnect)
                    {
                        // If the job cannot be disconnected then add it to
                        // the stop list.
                        throttleManager.AddOperation(new StopJobOperationHelper(job));
                    }
                    else if (job.JobStateInfo.State == JobState.Running)
                    {
                        // Otherwise add disconnectable runspaces to list so that
                        // they can be disconnected.
                        IEnumerable<RemoteRunspace> jobRunspaces = job.GetRunspaces();
                        if (jobRunspaces != null)
                        {
                            disconnectRunspaces.AddRange(jobRunspaces);
                        }
                    }
                } // foreach job

                // Stop jobs.
                throttleManager.EndSubmitOperations();
                jobsStopCompleted.WaitOne();
            } // using jobsStopCompleted

            // Disconnect all disconnectable job runspaces found.
            CloseOrDisconnectAllRemoteRunspaces(() =>
                {
                    return disconnectRunspaces;
                });
        }
Exemple #5
0
        /// <summary>
        /// Closes or disconnects all the remote runspaces passed in by the getRunspace
        /// function.  If a remote runspace supports disconnect then it will be disconnected 
        /// rather than closed.
        /// </summary>
        private void CloseOrDisconnectAllRemoteRunspaces(Func<List<RemoteRunspace>> getRunspaces)
        {
            List<RemoteRunspace> runspaces = getRunspaces();
            if (runspaces.Count == 0) { return; }

            // whether the close of all remoterunspaces completed
            using (ManualResetEvent remoteRunspaceCloseCompleted = new ManualResetEvent(false))
            {
                ThrottleManager throttleManager = new ThrottleManager();
                throttleManager.ThrottleComplete += delegate (object sender, EventArgs e)
                {
                    remoteRunspaceCloseCompleted.Set();
                };

                foreach (RemoteRunspace remoteRunspace in runspaces)
                {
                    IThrottleOperation operation = new CloseOrDisconnectRunspaceOperationHelper(remoteRunspace);
                    throttleManager.AddOperation(operation);
                }
                throttleManager.EndSubmitOperations();

                remoteRunspaceCloseCompleted.WaitOne();
            }
        }
Exemple #6
0
 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();
         }
     }
 }
Exemple #7
0
 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);
     }
 }
Exemple #8
0
 internal static void AddOperation(object operation, ThrottleManager throttleManager)
 {
     throttleManager.AddOperation((IThrottleOperation) operation);
 }