예제 #1
0
 private void OperationCompleteHandler(object source, OperationStateEventArgs stateEventArgs)
 {
     lock (this.syncObject)
     {
         IThrottleOperation item = source as IThrottleOperation;
         int index = -1;
         if (stateEventArgs.OperationState == OperationState.StartComplete)
         {
             index = this.startOperationQueue.IndexOf(item);
             if (index != -1)
             {
                 this.startOperationQueue.RemoveAt(index);
             }
         }
         else
         {
             index = this.startOperationQueue.IndexOf(item);
             if (index != -1)
             {
                 this.startOperationQueue.RemoveAt(index);
             }
             index = this.stopOperationQueue.IndexOf(item);
             if (index != -1)
             {
                 this.stopOperationQueue.RemoveAt(index);
             }
             item.IgnoreStop = true;
         }
     }
     this.RaiseThrottleManagerEvents();
     this.StartOneOperationFromQueue();
 }
예제 #2
0
 private void OperationCompleteHandler(object source, OperationStateEventArgs stateEventArgs)
 {
     lock (this.syncObject)
     {
         IThrottleOperation item = source as IThrottleOperation;
         int index = -1;
         if (stateEventArgs.OperationState == OperationState.StartComplete)
         {
             index = this.startOperationQueue.IndexOf(item);
             if (index != -1)
             {
                 this.startOperationQueue.RemoveAt(index);
             }
         }
         else
         {
             index = this.startOperationQueue.IndexOf(item);
             if (index != -1)
             {
                 this.startOperationQueue.RemoveAt(index);
             }
             index = this.stopOperationQueue.IndexOf(item);
             if (index != -1)
             {
                 this.stopOperationQueue.RemoveAt(index);
             }
             item.IgnoreStop = true;
         }
     }
     this.RaiseThrottleManagerEvents();
     this.StartOneOperationFromQueue();
 }
예제 #3
0
 private void SendStopComplete(EventArgs eventArgs = null)
 {
     OperationStateEventArgs args = new OperationStateEventArgs {
         BaseEvent = eventArgs,
         OperationState = OperationState.StopComplete
     };
     this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, args);
 }
예제 #4
0
 private void RaiseOperationCompleteEvent()
 {
     this.job.StateChanged -= new EventHandler<JobStateEventArgs>(this.HandleJobStateChanged);
     OperationStateEventArgs eventArgs = new OperationStateEventArgs {
         OperationState = OperationState.StartComplete,
         BaseEvent = EventArgs.Empty
     };
     this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, eventArgs);
 }
예제 #5
0
        private void WorkerThreadMethodStop()
        {
            this.workerThreadStart.Abort();
            OperationStateEventArgs eventArgs = new OperationStateEventArgs {
                OperationState = OperationState.StopComplete
            };

            this.OperationComplete.SafeInvoke <OperationStateEventArgs>(this, eventArgs);
        }
예제 #6
0
파일: Operation.cs 프로젝트: nickchal/pash
 private void WorkerThreadMethodStart()
 {
     Thread.Sleep(this.sleepTime);
     this.done = true;
     OperationStateEventArgs eventArgs = new OperationStateEventArgs {
         OperationState = OperationState.StartComplete
     };
     this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, eventArgs);
 }
        /// <summary>
        /// Handler which handles state change for the object which implements
        /// the <see cref="System.Management.Automation.Remoting.IThrottleOperation"/>
        /// interface.
        /// </summary>
        /// <param name="source">Sender of the event.</param>
        /// <param name="stateEventArgs">Event information object which describes the event
        /// which triggered this method</param>
        private void OperationCompleteHandler(object source, OperationStateEventArgs stateEventArgs)
        {
            // An item has completed operation. If it's a start operation which completed
            // remove the instance from the startOperationqueue. If it's a stop operation
            // which completed, then remove the instance from both queues
            lock (_syncObject)
            {
                IThrottleOperation operation = source as IThrottleOperation;

                Dbg.Assert(operation != null, "Source of event should not be null");

                int index = -1;

                if (stateEventArgs.OperationState == OperationState.StartComplete)
                {
                    // A stop operation can be initiated before a start operation completes.
                    // A stop operation handler cleans up an outstanding start operation.
                    // So it is possible that a start operation complete callback will find the
                    // operation removed from the queue by an earlier stop operation complete.
                    index = _startOperationQueue.IndexOf(operation);
                    if (index != -1)
                    {
                        _startOperationQueue.RemoveAt(index);
                    }
                }
                else
                {
                    // for a stop operation, the same operation object would have been
                    // added to the stopOperationQueue as well. So we need to
                    // remove both the instances.
                    index = _startOperationQueue.IndexOf(operation);
                    if (index != -1)
                    {
                        _startOperationQueue.RemoveAt(index);
                    }

                    index = _stopOperationQueue.IndexOf(operation);
                    if (index != -1)
                    {
                        _stopOperationQueue.RemoveAt(index);
                    }

                    // if an operation signals a stopcomplete, it can mean
                    // that the operation has completed. In this case, we
                    // need to set the isStopped to true
                    operation.IgnoreStop = true;
                }
            }

            // It's possible that all operations are completed at this point
            // and submit is complete. So raise event
            RaiseThrottleManagerEvents();

            // Do necessary things for starting operation for the next item in the queue
            StartOneOperationFromQueue();
        }
        private void WorkerThreadMethodStart()
        {
            Thread.Sleep(SleepTime);
            Done = true;
            OperationStateEventArgs operationStateEventArgs =
                new OperationStateEventArgs();

            operationStateEventArgs.OperationState = OperationState.StartComplete;
            OperationComplete.SafeInvoke(this, operationStateEventArgs);
        }
예제 #9
0
        private void WorkerThreadMethodStart()
        {
            Thread.Sleep(this.sleepTime);
            this.done = true;
            OperationStateEventArgs eventArgs = new OperationStateEventArgs {
                OperationState = OperationState.StartComplete
            };

            this.OperationComplete.SafeInvoke <OperationStateEventArgs>(this, eventArgs);
        }
        private void WorkerThreadMethodStop()
        {
            workerThreadStart.Abort();

            OperationStateEventArgs operationStateEventArgs =
                new OperationStateEventArgs();

            operationStateEventArgs.OperationState = OperationState.StopComplete;
            OperationComplete.SafeInvoke(this, operationStateEventArgs);
        }
예제 #11
0
 private void RaiseOperationCompleteEvent(EventArgs baseEventArgs)
 {
     if (base.pipeline != null)
     {
         base.pipeline.StateChanged -= new EventHandler<PipelineStateEventArgs>(this.HandlePipelineStateChanged);
         base.pipeline.Dispose();
     }
     OperationStateEventArgs eventArgs = new OperationStateEventArgs {
         OperationState = OperationState.StopComplete,
         BaseEvent = baseEventArgs
     };
     if (this.OperationComplete != null)
     {
         this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, eventArgs);
     }
 }
예제 #12
0
 private void FireEvent(OperationStateEventArgs operationStateEventArgs)
 {
     EventHandler<OperationStateEventArgs>[] handlerArray;
     lock (this._internalCallbacks)
     {
         handlerArray = new EventHandler<OperationStateEventArgs>[this._internalCallbacks.Count];
         this._internalCallbacks.CopyTo(handlerArray);
     }
     foreach (EventHandler<OperationStateEventArgs> handler in handlerArray)
     {
         try
         {
             handler.SafeInvoke<OperationStateEventArgs>(this, operationStateEventArgs);
         }
         catch (Exception exception)
         {
             CommandProcessorBase.CheckForSevereException(exception);
         }
     }
 }
예제 #13
0
        /// <summary>
        /// Handler for handling runspace state changed events. This method will be
        /// registered in the StartOperation and StopOperation methods. This handler
        /// will in turn invoke the OperationComplete event for all events that are 
        /// necessary - Opened, Closed, Disconnected, Broken. It will ignore all other state 
        /// changes.
        /// </summary>
        /// <remarks>
        /// There are two problems that need to be handled.
        /// 1) We need to make sure that the ThrottleManager StartComplete and StopComplete
        ///    operation events are called or the ThrottleManager will never end (hang).
        /// 2) The HandleRunspaceStateChanged event handler remains in the Runspace
        ///    StateChanged event call chain until this object is disposed.  We have to
        ///    disallow the HandleRunspaceStateChanged event from running and throwing
        ///    an exception since this prevents other event handlers in the chain from
        ///    being called.
        /// </remarks>
        /// <param name="source">Source of this event</param>
        /// <param name="stateEventArgs">object describing state information of the
        /// runspace</param>
        private void HandleRunspaceStateChanged(object source, RunspaceStateEventArgs stateEventArgs)
        {
            // Disregard intermediate states.
            switch (stateEventArgs.RunspaceStateInfo.State)
            {
                case RunspaceState.Opening:
                case RunspaceState.BeforeOpen:
                case RunspaceState.Closing:
                    return;
            }

            OperationStateEventArgs operationStateEventArgs = null;
            lock (_syncObject)
            {
                // We must call OperationComplete ony *once* for each Start/Stop operation.
                if (!_stopComplete)
                {
                    // Note that the StopComplete callback removes *both* the Start and Stop
                    // operations from their respective queues.  So update the member vars
                    // accordingly.
                    _stopComplete = true;
                    _startComplete = true;
                    operationStateEventArgs = new OperationStateEventArgs();
                    operationStateEventArgs.BaseEvent = stateEventArgs;
                    operationStateEventArgs.OperationState = OperationState.StopComplete;
                }
                else if (!_startComplete)
                {
                    _startComplete = true;
                    operationStateEventArgs = new OperationStateEventArgs();
                    operationStateEventArgs.BaseEvent = stateEventArgs;
                    operationStateEventArgs.OperationState = OperationState.StartComplete;
                }
            }

            if (operationStateEventArgs != null)
            {
                // Fire callbacks in list order.
                FireEvent(operationStateEventArgs);
            }
        }
예제 #14
0
        } // StartOperation

        /// <summary>
        /// Closes the runspace already opened asynchronously
        /// </summary>        
        internal override void StopOperation()
        {
            OperationStateEventArgs operationStateEventArgs = null;

            lock (_syncObject)
            {
                // Ignore stop operation if start operation has completed.
                if (_startComplete)
                {
                    _stopComplete = true;
                    _startComplete = true;
                    operationStateEventArgs = new OperationStateEventArgs();
                    operationStateEventArgs.BaseEvent = new RunspaceStateEventArgs(OperatedRunspace.RunspaceStateInfo);
                    operationStateEventArgs.OperationState = OperationState.StopComplete;
                }
                else
                {
                    _stopComplete = false;
                }
            }

            if (operationStateEventArgs != null)
            {
                FireEvent(operationStateEventArgs);
            }
            else
            {
                OperatedRunspace.CloseAsync();
            }
        }
예제 #15
0
		private void HandleOperationComplete(object sender, OperationStateEventArgs stateEventArgs)
		{
			WmiAsyncCmdletHelper wmiAsyncCmdletHelper = (WmiAsyncCmdletHelper)sender;
			if (wmiAsyncCmdletHelper.State != WmiState.NotStarted)
			{
				if (wmiAsyncCmdletHelper.State != WmiState.Running)
				{
					if (wmiAsyncCmdletHelper.State != WmiState.Completed)
					{
						if (wmiAsyncCmdletHelper.State != WmiState.Failed)
						{
							base.SetJobState(JobState.Stopped, wmiAsyncCmdletHelper.InternalException);
							return;
						}
						else
						{
							base.SetJobState(JobState.Failed, wmiAsyncCmdletHelper.InternalException);
							return;
						}
					}
					else
					{
						base.SetJobState(JobState.Completed, wmiAsyncCmdletHelper.InternalException);
						return;
					}
				}
				else
				{
					base.SetJobState(JobState.Running, wmiAsyncCmdletHelper.InternalException);
					return;
				}
			}
			else
			{
				base.SetJobState(JobState.Stopped, wmiAsyncCmdletHelper.InternalException);
				return;
			}
		}
예제 #16
0
 private void SendStopComplete()
 {
     OperationStateEventArgs operationStateEventArg = new OperationStateEventArgs();
     operationStateEventArg.OperationState = OperationState.StopComplete;
     this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, operationStateEventArg);
 }
예제 #17
0
 /// <summary>
 /// Raise operation completion event
 /// </summary>
 internal void RaiseOperationCompleteEvent(EventArgs baseEventArgs, OperationState state)
 {
     OperationStateEventArgs operationStateEventArgs = new OperationStateEventArgs();
     operationStateEventArgs.OperationState = state;
     OperationComplete.SafeInvoke(this, operationStateEventArgs);
 } // RaiseOperationCompleteEvent
예제 #18
0
 private void WorkerThreadMethodStart()
 {
     Thread.Sleep(SleepTime);
     Done = true;
     OperationStateEventArgs operationStateEventArgs =
             new OperationStateEventArgs();
     operationStateEventArgs.OperationState = OperationState.StartComplete;
     OperationComplete.SafeInvoke(this, operationStateEventArgs);
 }
예제 #19
0
        private void HandleRunspaceStateChanged(object sender, OperationStateEventArgs stateEventArgs)
        {
            ErrorRecord errorRecord;
            PSRemotingTransportException exception2;
            string str;
            if (sender == null)
            {
                throw PSTraceSource.NewArgumentNullException("sender");
            }
            if (stateEventArgs == null)
            {
                throw PSTraceSource.NewArgumentNullException("stateEventArgs");
            }
            RunspaceStateEventArgs baseEvent = stateEventArgs.BaseEvent as RunspaceStateEventArgs;
            RunspaceState state = baseEvent.RunspaceStateInfo.State;
            OpenRunspaceOperation operation = sender as OpenRunspaceOperation;
            RemoteRunspace operatedRunspace = operation.OperatedRunspace;
            if (operatedRunspace != null)
            {
                operatedRunspace.URIRedirectionReported -= new EventHandler<RemoteDataEventArgs<Uri>>(this.HandleURIDirectionReported);
            }
            PipelineWriter objectWriter = this.stream.ObjectWriter;
            Exception reason = baseEvent.RunspaceStateInfo.Reason;
            switch (state)
            {
                case RunspaceState.Opened:
                {
                    PSSession remoteRunspaceInfo = new PSSession(operatedRunspace);
                    base.RunspaceRepository.Add(remoteRunspaceInfo);
                    Action<Cmdlet> action = cmdlet => cmdlet.WriteObject(remoteRunspaceInfo);
                    if (objectWriter.IsOpen)
                    {
                        objectWriter.Write(action);
                    }
                    return;
                }
                case RunspaceState.Closed:
                {
                    Uri uri = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<Uri>(operatedRunspace.ConnectionInfo, "ConnectionUri", null);
                    string message = base.GetMessage(RemotingErrorIdStrings.RemoteRunspaceClosed, new object[] { (uri != null) ? uri.AbsoluteUri : string.Empty });
                    Action<Cmdlet> action3 = cmdlet => cmdlet.WriteVerbose(message);
                    if (objectWriter.IsOpen)
                    {
                        objectWriter.Write(action3);
                    }
                    if (reason != null)
                    {
                        ErrorRecord errorRecord2 = new ErrorRecord(reason, "PSSessionStateClosed", ErrorCategory.OpenError, operatedRunspace);
                        Action<Cmdlet> action4 = cmdlet => cmdlet.WriteError(errorRecord2);
                        if (objectWriter.IsOpen)
                        {
                            objectWriter.Write(action4);
                        }
                    }
                    return;
                }
                case RunspaceState.Closing:
                    return;

                case RunspaceState.Broken:
                    exception2 = reason as PSRemotingTransportException;
                    str = null;
                    if (exception2 != null)
                    {
                        OpenRunspaceOperation operation2 = sender as OpenRunspaceOperation;
                        if (operation2 != null)
                        {
                            string computerName = operation2.OperatedRunspace.ConnectionInfo.ComputerName;
                            if (exception2.ErrorCode != -2144108135)
                            {
                                str = "[" + computerName + "] ";
                                if (!string.IsNullOrEmpty(exception2.Message))
                                {
                                    str = str + exception2.Message;
                                }
                                else if (!string.IsNullOrEmpty(exception2.TransportMessage))
                                {
                                    str = str + exception2.TransportMessage;
                                }
                                break;
                            }
                            string str3 = PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.URIRedirectionReported, new object[] { exception2.Message, "MaximumConnectionRedirectionCount", "PSSessionOption", "AllowRedirection" });
                            str = "[" + computerName + "] " + str3;
                        }
                    }
                    break;

                default:
                    return;
            }
            PSRemotingDataStructureException exception3 = reason as PSRemotingDataStructureException;
            if (exception3 != null)
            {
                OpenRunspaceOperation operation3 = sender as OpenRunspaceOperation;
                if (operation3 != null)
                {
                    string str4 = operation3.OperatedRunspace.ConnectionInfo.ComputerName;
                    str = "[" + str4 + "] " + exception3.Message;
                }
            }
            if (reason == null)
            {
                reason = new RuntimeException(base.GetMessage(RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState, new object[] { state }));
            }
            string fQEIDFromTransportError = WSManTransportManagerUtils.GetFQEIDFromTransportError((exception2 != null) ? exception2.ErrorCode : 0, this._defaultFQEID);
            errorRecord = new ErrorRecord(reason, operatedRunspace, fQEIDFromTransportError, ErrorCategory.OpenError, null, null, null, null, null, str, null);
            Action<Cmdlet> action2 = cmdlet => cmdlet.WriteError(errorRecord);
            if (objectWriter.IsOpen)
            {
                objectWriter.Write(action2);
            }
            this.toDispose.Add(operatedRunspace);
        }
예제 #20
0
 internal override void StopOperation()
 {
     OperationStateEventArgs operationStateEventArgs = null;
     lock (this._syncObject)
     {
         if (this.startComplete)
         {
             this.stopComplete = true;
             this.startComplete = true;
             operationStateEventArgs = new OperationStateEventArgs {
                 BaseEvent = new RunspaceStateEventArgs(this.runspace.RunspaceStateInfo),
                 OperationState = OperationState.StopComplete
             };
         }
         else
         {
             this.stopComplete = false;
         }
     }
     if (operationStateEventArgs != null)
     {
         this.FireEvent(operationStateEventArgs);
     }
     else
     {
         this.runspace.CloseAsync();
     }
 }
예제 #21
0
파일: Operation.cs 프로젝트: nickchal/pash
 private void WorkerThreadMethodStop()
 {
     this.workerThreadStart.Abort();
     OperationStateEventArgs eventArgs = new OperationStateEventArgs {
         OperationState = OperationState.StopComplete
     };
     this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, eventArgs);
 }
예제 #22
0
파일: Operation.cs 프로젝트: nickchal/pash
 private void Operation_OperationComplete(object sender, OperationStateEventArgs e)
 {
     this.InternalEvent.SafeInvoke<EventArgs>(sender, e);
 }
예제 #23
0
 protected virtual void HandleOperationComplete(object sender, OperationStateEventArgs stateEventArgs)
 {
     ExecutionCmdletHelper helper = sender as ExecutionCmdletHelper;
     this.DeterminedAndSetJobState(helper);
 }
예제 #24
0
 protected override void HandleOperationComplete(object sender, OperationStateEventArgs stateEventArgs)
 {
     Exception exception;
     ErrorRecord record;
     ExecutionCmdletHelper helper = sender as ExecutionCmdletHelper;
     base.ProcessJobFailure(helper, out exception, out record);
     if (record != null)
     {
         this.WriteError(record);
     }
 }
예제 #25
0
 private void FireEvent(OperationStateEventArgs operationStateEventArgs)
 {
     EventHandler<OperationStateEventArgs>[] copyCallbacks;
     lock (_internalCallbacks)
     {
         copyCallbacks = new EventHandler<OperationStateEventArgs>[_internalCallbacks.Count];
         _internalCallbacks.CopyTo(copyCallbacks);
     }
     foreach (var callbackDelegate in copyCallbacks)
     {
         // Ensure all callbacks get called to prevent ThrottleManager hang.
         try
         {
             callbackDelegate.SafeInvoke(this, operationStateEventArgs);
         }
         catch (Exception e)
         {
             CommandProcessorBase.CheckForSevereException(e);
         }
     }
 }
예제 #26
0
        /// <summary>
        /// Handles state changes for Runspace
        /// </summary>
        /// <param name="sender">Sender of this event</param>
        /// <param name="stateEventArgs">Event information object which describes
        /// the event which triggered this method</param>
        private void HandleRunspaceStateChanged(object sender, OperationStateEventArgs stateEventArgs)
        {
            if (sender == null)
            {
                throw PSTraceSource.NewArgumentNullException("sender");
            }

            if (stateEventArgs == null)
            {
                throw PSTraceSource.NewArgumentNullException("stateEventArgs");
            }

            RunspaceStateEventArgs runspaceStateEventArgs =
                        stateEventArgs.BaseEvent as RunspaceStateEventArgs;
            RunspaceStateInfo stateInfo = runspaceStateEventArgs.RunspaceStateInfo;
            RunspaceState state = stateInfo.State;
            OpenRunspaceOperation operation = sender as OpenRunspaceOperation;
            RemoteRunspace remoteRunspace = operation.OperatedRunspace;

            // since we got state changed event..we dont need to listen on
            // URI redirections anymore
            if (null != remoteRunspace)
            {
                remoteRunspace.URIRedirectionReported -= HandleURIDirectionReported;
            }

            PipelineWriter writer = _stream.ObjectWriter;
            Exception reason = runspaceStateEventArgs.RunspaceStateInfo.Reason;

            switch (state)
            {
                case RunspaceState.Opened:
                    {
                        // Indicates that runspace is successfully opened
                        // Write it to PipelineWriter to be handled in 
                        // HandleRemoteRunspace
                        PSSession remoteRunspaceInfo = new PSSession(remoteRunspace);

                        this.RunspaceRepository.Add(remoteRunspaceInfo);

                        Action<Cmdlet> outputWriter = delegate (Cmdlet cmdlet)
                        {
                            cmdlet.WriteObject(remoteRunspaceInfo);
                        };
                        if (writer.IsOpen)
                        {
                            writer.Write(outputWriter);
                        }
                    }
                    break;

                case RunspaceState.Broken:
                    {
                        // Open resulted in a broken state. Extract reason
                        // and write an error record

                        // set the transport message in the error detail so that
                        // the user can directly get to see the message without
                        // having to mine through the error record details
                        PSRemotingTransportException transException =
                            reason as PSRemotingTransportException;
                        String errorDetails = null;
                        int transErrorCode = 0;
                        if (transException != null)
                        {
                            OpenRunspaceOperation senderAsOp = sender as OpenRunspaceOperation;
                            transErrorCode = transException.ErrorCode;
                            if (senderAsOp != null)
                            {
                                String host = senderAsOp.OperatedRunspace.ConnectionInfo.ComputerName;

                                if (transException.ErrorCode ==
                                    System.Management.Automation.Remoting.Client.WSManNativeApi.ERROR_WSMAN_REDIRECT_REQUESTED)
                                {
                                    // Handling a special case for redirection..we should talk about
                                    // AllowRedirection parameter and WSManMaxRedirectionCount preference
                                    // variables
                                    string message = PSRemotingErrorInvariants.FormatResourceString(
                                        RemotingErrorIdStrings.URIRedirectionReported,
                                        transException.Message,
                                        "MaximumConnectionRedirectionCount",
                                        Microsoft.PowerShell.Commands.PSRemotingBaseCmdlet.DEFAULT_SESSION_OPTION,
                                        "AllowRedirection");

                                    errorDetails = "[" + host + "] " + message;
                                }
                                else
                                {
                                    errorDetails = "[" + host + "] ";
                                    if (!String.IsNullOrEmpty(transException.Message))
                                    {
                                        errorDetails += transException.Message;
                                    }
                                    else if (!String.IsNullOrEmpty(transException.TransportMessage))
                                    {
                                        errorDetails += transException.TransportMessage;
                                    }
                                }
                            }
                        }

                        // add host identification information in data structure handler message
                        PSRemotingDataStructureException protoExeption = reason as PSRemotingDataStructureException;

                        if (protoExeption != null)
                        {
                            OpenRunspaceOperation senderAsOp = sender as OpenRunspaceOperation;

                            if (senderAsOp != null)
                            {
                                String host = senderAsOp.OperatedRunspace.ConnectionInfo.ComputerName;

                                errorDetails = "[" + host + "] " + protoExeption.Message;
                            }
                        }

                        if (reason == null)
                        {
                            reason = new RuntimeException(this.GetMessage(RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState, state));
                        }

                        string fullyQualifiedErrorId = WSManTransportManagerUtils.GetFQEIDFromTransportError(
                            transErrorCode,
                            _defaultFQEID);

                        if (WSManNativeApi.ERROR_WSMAN_NO_LOGON_SESSION_EXIST == transErrorCode)
                        {
                            errorDetails += System.Environment.NewLine + String.Format(System.Globalization.CultureInfo.CurrentCulture, RemotingErrorIdStrings.RemotingErrorNoLogonSessionExist);
                        }
                        ErrorRecord errorRecord = new ErrorRecord(reason,
                             remoteRunspace, fullyQualifiedErrorId,
                                   ErrorCategory.OpenError, null, null,
                                        null, null, null, errorDetails, null);

                        Action<Cmdlet> errorWriter = delegate (Cmdlet cmdlet)
                        {
                            //
                            // In case of PSDirectException, we should output the precise error message
                            // in inner exception instead of the generic one in outer exception.
                            //
                            if ((errorRecord.Exception != null) &&
                                (errorRecord.Exception.InnerException != null))
                            {
                                PSDirectException ex = errorRecord.Exception.InnerException as PSDirectException;
                                if (ex != null)
                                {
                                    errorRecord = new ErrorRecord(errorRecord.Exception.InnerException,
                                                                  errorRecord.FullyQualifiedErrorId,
                                                                  errorRecord.CategoryInfo.Category,
                                                                  errorRecord.TargetObject);
                                }
                            }

                            cmdlet.WriteError(errorRecord);
                        };
                        if (writer.IsOpen)
                        {
                            writer.Write(errorWriter);
                        }

                        _toDispose.Add(remoteRunspace);
                    }
                    break;

                case RunspaceState.Closed:
                    {
                        // The runspace was closed possibly because the user
                        // hit ctrl-C when runspaces were being opened or Dispose has been
                        // called when there are open runspaces
                        Uri connectionUri = WSManConnectionInfo.ExtractPropertyAsWsManConnectionInfo<Uri>(remoteRunspace.ConnectionInfo,
                            "ConnectionUri", null);
                        String message =
                            GetMessage(RemotingErrorIdStrings.RemoteRunspaceClosed,
                                        (connectionUri != null) ?
                                        connectionUri.AbsoluteUri : string.Empty);

                        Action<Cmdlet> verboseWriter = delegate (Cmdlet cmdlet)
                        {
                            cmdlet.WriteVerbose(message);
                        };
                        if (writer.IsOpen)
                        {
                            writer.Write(verboseWriter);
                        }

                        // runspace may not have been opened in certain cases
                        // like when the max memory is set to 25MB, in such 
                        // cases write an error record
                        if (reason != null)
                        {
                            ErrorRecord errorRecord = new ErrorRecord(reason,
                                 "PSSessionStateClosed",
                                       ErrorCategory.OpenError, remoteRunspace);

                            Action<Cmdlet> errorWriter = delegate (Cmdlet cmdlet)
                            {
                                cmdlet.WriteError(errorRecord);
                            };
                            if (writer.IsOpen)
                            {
                                writer.Write(errorWriter);
                            }
                        }
                    }
                    break;
            }// switch
        } // HandleRunspaceStateChanged
예제 #27
0
        private void WorkerThreadMethodStop()
        {
            workerThreadStart.Abort();

            OperationStateEventArgs operationStateEventArgs =
                    new OperationStateEventArgs();
            operationStateEventArgs.OperationState = OperationState.StopComplete;
            OperationComplete.SafeInvoke(this, operationStateEventArgs);
        }
예제 #28
0
        } // RaiseOperationCompleteEvent

        /// <summary>
        /// Raise an operation complete event.
        /// </summary>
        /// <param name="baseEventArgs">The event args which actually
        /// raises this operation complete</param>
        private void RaiseOperationCompleteEvent(EventArgs baseEventArgs)
        {
            if (pipeline != null)
            {
                // Dispose the pipeline object and release data and remoting resources.
                // Pipeline object remains to provide information on final state and any errors incurred.
                pipeline.StateChanged -= new EventHandler<PipelineStateEventArgs>(HandlePipelineStateChanged);
                pipeline.Dispose();
            }

            if (RemoteRunspace != null)
            {
                // Dispose of the runspace object.
                RemoteRunspace.Dispose();
                RemoteRunspace = null;
            }

            OperationStateEventArgs operationStateEventArgs =
                    new OperationStateEventArgs();
            operationStateEventArgs.OperationState =
                    OperationState.StopComplete;
            operationStateEventArgs.BaseEvent = baseEventArgs;
            OperationComplete.SafeInvoke(this, operationStateEventArgs);
        } // RaiseOperationCompleteEvent
예제 #29
0
 private void SendStopComplete()
 {
     OperationStateEventArgs operationStateEventArgs = new OperationStateEventArgs();
     operationStateEventArgs.OperationState = OperationState.StopComplete;
     OperationComplete.SafeInvoke(this, operationStateEventArgs);
 }
예제 #30
0
        }// ThrottleManager

        #endregion Constructors

        #region Private Methods        

        /// <summary>
        /// Handler which handles state change for the object which implements
        /// the <see cref="System.Management.Automation.Remoting.IThrottleOperation"/>
        /// interface
        /// </summary>
        /// <param name="source">sender of the event</param>
        /// <param name="stateEventArgs">Event information object which describes the event
        /// which triggered this method</param>
        private void OperationCompleteHandler(object source, OperationStateEventArgs stateEventArgs)
        {
            // An item has completed operation. If it's a start operation which completed
            // remove the instance from the startOperationqueue. If it's a stop operation 
            // which completed, then remove the instance from both queues
            lock (_syncObject)
            {
                IThrottleOperation operation = source as IThrottleOperation;

                Dbg.Assert(operation != null, "Source of event should not be null");

                int index = -1;

                if (stateEventArgs.OperationState == OperationState.StartComplete)
                {
                    // A stop operation can be initiated before a start operation completes.
                    // A stop operation handler cleans up an outstanding start operation.
                    // So it is possible that a start operation complete callback will find the 
                    // operation removed from the queue by an earlier stop operation complete.
                    index = _startOperationQueue.IndexOf(operation);
                    if (index != -1)
                    {
                        _startOperationQueue.RemoveAt(index);
                    }
                }
                else
                {
                    // for a stop operation, the same operation object would have been
                    // added to the stopOperationQueue as well. So we need to 
                    // remove both the instances.
                    index = _startOperationQueue.IndexOf(operation);
                    if (index != -1)
                    {
                        _startOperationQueue.RemoveAt(index);
                    }

                    index = _stopOperationQueue.IndexOf(operation);
                    if (index != -1)
                    {
                        _stopOperationQueue.RemoveAt(index);
                    }

                    // if an operation signals a stopcomplete, it can mean
                    // that the operation has completed. In this case, we
                    // need to set the isStopped to true
                    operation.IgnoreStop = true;
                }
            }

            // It's possible that all operations are completed at this point
            // and submit is complete. So raise event
            RaiseThrottleManagerEvents();

            // Do necessary things for starting operation for the next item in the queue
            StartOneOperationFromQueue();
        } // OperationCompleteHandler
예제 #31
0
		internal void RaiseOperationCompleteEvent(EventArgs baseEventArgs, OperationState state)
		{
			OperationStateEventArgs operationStateEventArg = new OperationStateEventArgs();
			operationStateEventArg.OperationState = state;
			this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, operationStateEventArg);
		}
예제 #32
0
        /// <summary>
        /// Handles operation complete event
        /// </summary>
        private void HandleOperationComplete(object sender, OperationStateEventArgs stateEventArgs)
        {
            WmiAsyncCmdletHelper helper = (WmiAsyncCmdletHelper)sender;

            if (helper.State == WmiState.NotStarted)
            {
                //This is a case WMI operation was not started.
                SetJobState(JobState.Stopped, helper.InternalException);
            }
            else if (helper.State == WmiState.Running)
            {
                SetJobState(JobState.Running, helper.InternalException);
            }
            else if (helper.State == WmiState.Completed)
            {
                SetJobState(JobState.Completed, helper.InternalException);
            }
            else if (helper.State == WmiState.Failed)
            {
                SetJobState(JobState.Failed, helper.InternalException);
            }
            else
            {
                SetJobState(JobState.Stopped, helper.InternalException);
            }
        }
예제 #33
0
        /// <summary>
        /// Raise the operation completed event
        /// </summary>
        private void RaiseOperationCompleteEvent()
        {
            _remoteRunspace.StateChanged -= new EventHandler<RunspaceStateEventArgs>(HandleRunspaceStateChanged);

            OperationStateEventArgs operationStateEventArgs =
                    new OperationStateEventArgs();
            operationStateEventArgs.OperationState =
                    OperationState.StartComplete;
            operationStateEventArgs.BaseEvent = EventArgs.Empty;

            OperationComplete.SafeInvoke(this, operationStateEventArgs);
        }
예제 #34
0
 private void SendStopComplete()
 {
     OperationStateEventArgs eventArgs = new OperationStateEventArgs {
         OperationState = OperationState.StopComplete
     };
     this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, eventArgs);
 }
예제 #35
0
 private void HandleRunspaceStateChanged(object source, RunspaceStateEventArgs stateEventArgs)
 {
     switch (stateEventArgs.RunspaceStateInfo.State)
     {
         case RunspaceState.BeforeOpen:
         case RunspaceState.Opening:
         case RunspaceState.Closing:
             return;
     }
     OperationStateEventArgs operationStateEventArgs = null;
     lock (this._syncObject)
     {
         if (!this.stopComplete)
         {
             this.stopComplete = true;
             this.startComplete = true;
             operationStateEventArgs = new OperationStateEventArgs {
                 BaseEvent = stateEventArgs,
                 OperationState = OperationState.StopComplete
             };
         }
         else if (!this.startComplete)
         {
             this.startComplete = true;
             operationStateEventArgs = new OperationStateEventArgs {
                 BaseEvent = stateEventArgs,
                 OperationState = OperationState.StartComplete
             };
         }
     }
     if (operationStateEventArgs != null)
     {
         this.FireEvent(operationStateEventArgs);
     }
 }
 private void Operation_OperationComplete(object sender, OperationStateEventArgs e)
 {
     InternalEvent.SafeInvoke(sender, e);
 }
예제 #37
0
 internal override void StopOperation()
 {
     this.RemoveEventCallback();
     OperationStateEventArgs eventArgs = new OperationStateEventArgs {
         OperationState = OperationState.StopComplete
     };
     this.OperationComplete.SafeInvoke<OperationStateEventArgs>(this, eventArgs);
 }