internal void ServicePendingCallbacks(object objectToProcess)
 {
     tracer.WriteLine("ServicePendingCallbacks thread is starting", new object[0]);
     PSEtwLog.ReplaceActivityIdForCurrentThread(this.runspacePoolInstanceId, PSEventId.OperationalTransferEventRunspacePool, PSEventId.AnalyticTransferEventRunspacePool, PSKeyword.Transport, PSTask.None);
     try
     {
         while (!this.isClosed)
         {
             CallbackNotificationInformation information = null;
             lock (this.callbackNotificationQueue)
             {
                 if ((this.callbackNotificationQueue.Count <= 0) || this.suspendQueueServicing)
                 {
                     return;
                 }
                 information = this.callbackNotificationQueue.Dequeue();
             }
             if (information != null)
             {
                 if (information.transportError != null)
                 {
                     this.RaiseErrorHandler(information.transportError);
                     return;
                 }
                 if (information.privateData != null)
                 {
                     this.ProcessPrivateData(information.privateData);
                 }
                 else
                 {
                     base.OnDataAvailableCallback(information.remoteObject);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         tracer.WriteLine(string.Format(CultureInfo.InvariantCulture, "Exception processing data. {0}", new object[] { exception.Message }), new object[0]);
         PSRemotingTransportException   e         = new PSRemotingTransportException(exception.Message, exception);
         TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.ReceiveShellOutputEx);
         this.RaiseErrorHandler(eventArgs);
     }
     finally
     {
         lock (this.callbackNotificationQueue)
         {
             tracer.WriteLine("ServicePendingCallbacks thread is exiting", new object[0]);
             this.isServicingCallbacks = false;
             this.EnqueueAndStartProcessingThread(null, null, null);
         }
     }
 }
 private void HandleClientRemoteSessionStateChanged(object sender, RemoteSessionStateEventArgs e)
 {
     if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSending)
     {
         if (this.createRunspaceCalled)
         {
             return;
         }
         lock (this.syncObject)
         {
             if (this.createRunspaceCalled)
             {
                 return;
             }
             this.createRunspaceCalled = true;
         }
         PSPrimitiveDictionary applicationArguments = PSPrimitiveDictionary.CloneAndAddPSVersionTable(this.applicationArguments);
         var createPoolData = RemotingEncoder.GenerateCreateRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces, this.remoteSession.RemoteRunspacePoolInternal, this.host, applicationArguments);
         this.SendDataAsync(createPoolData);
     }
     if (e.SessionStateInfo.State == RemoteSessionState.NegotiationSendingOnConnect)
     {
         this.SendDataAsync(RemotingEncoder.GenerateConnectRunspacePool(this.clientRunspacePoolId, this.minRunspaces, this.maxRunspaces));
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.ClosingConnection)
     {
         List <ClientPowerShellDataStructureHandler> list;
         Exception closingReason = this.closingReason;
         if (closingReason == null)
         {
             closingReason      = e.SessionStateInfo.Reason;
             this.closingReason = closingReason;
         }
         lock (this.associationSyncObject)
         {
             list = new List <ClientPowerShellDataStructureHandler>(this.associatedPowerShellDSHandlers.Values);
         }
         foreach (ClientPowerShellDataStructureHandler handler in list)
         {
             handler.CloseConnectionAsync(this.closingReason);
         }
         this.SessionClosing.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(closingReason));
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.Closed)
     {
         Exception reason = this.closingReason;
         if (reason == null)
         {
             reason             = e.SessionStateInfo.Reason;
             this.closingReason = reason;
         }
         if (reason != null)
         {
             this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Broken, reason));
         }
         else
         {
             this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Closed, reason));
         }
         this.SessionClosed.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(reason));
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.Connected)
     {
         PSEtwLog.ReplaceActivityIdForCurrentThread(this.clientRunspacePoolId, PSEventId.OperationalTransferEventRunspacePool, PSEventId.AnalyticTransferEventRunspacePool, PSKeyword.Runspace, PSTask.CreateRunspace);
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.Disconnected)
     {
         this.NotifyAssociatedPowerShells(new RunspacePoolStateInfo(RunspacePoolState.Disconnected, e.SessionStateInfo.Reason));
         this.SessionDisconnected.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(e.SessionStateInfo.Reason));
     }
     else if (this._reconnecting && (e.SessionStateInfo.State == RemoteSessionState.Established))
     {
         this.SessionReconnected.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(null));
         this._reconnecting = false;
     }
     else if (e.SessionStateInfo.State == RemoteSessionState.RCDisconnecting)
     {
         this.SessionRCDisconnecting.SafeInvoke <RemoteDataEventArgs <Exception> >(this, new RemoteDataEventArgs <Exception>(null));
     }
     else if (e.SessionStateInfo.Reason != null)
     {
         this.closingReason = e.SessionStateInfo.Reason;
     }
 }