private IAsyncResult CoreClose(bool isAsync, AsyncCallback callback, object asyncState)
 {
     lock (this.syncObject)
     {
         if (((this.stateInfo.State == RunspacePoolState.Closed) || (this.stateInfo.State == RunspacePoolState.Broken)) || (((this.stateInfo.State == RunspacePoolState.Closing) || (this.stateInfo.State == RunspacePoolState.Disconnecting)) || (this.stateInfo.State == RunspacePoolState.Disconnected)))
         {
             if (isAsync)
             {
                 RunspacePoolAsyncResult result = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false);
                 result.SetAsCompleted(null);
                 return(result);
             }
             return(null);
         }
         this.stateInfo = new System.Management.Automation.RunspacePoolStateInfo(RunspacePoolState.Closing, null);
     }
     this.RaiseStateChangeEvent(this.stateInfo);
     if (isAsync)
     {
         RunspacePoolAsyncResult state = new RunspacePoolAsyncResult(this.instanceId, callback, asyncState, false);
         ThreadPool.QueueUserWorkItem(new WaitCallback(this.CloseThreadProc), state);
         return(state);
     }
     this.CloseHelper();
     return(null);
 }
Example #2
0
        public override IAsyncResult BeginClose(AsyncCallback callback, object asyncState)
        {
            bool flag  = false;
            bool flag2 = false;
            RunspacePoolStateInfo   stateInfo        = new RunspacePoolStateInfo(RunspacePoolState.BeforeOpen, null);
            RunspacePoolAsyncResult closeAsyncResult = null;

            lock (base.syncObject)
            {
                if ((base.stateInfo.State == RunspacePoolState.Closed) || (base.stateInfo.State == RunspacePoolState.Broken))
                {
                    flag2            = true;
                    closeAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false);
                }
                else if (base.stateInfo.State == RunspacePoolState.BeforeOpen)
                {
                    stateInfo             = base.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closed, null);
                    flag                  = true;
                    flag2                 = true;
                    this.closeAsyncResult = null;
                    closeAsyncResult      = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false);
                }
                else if ((base.stateInfo.State == RunspacePoolState.Opened) || (base.stateInfo.State == RunspacePoolState.Opening))
                {
                    stateInfo             = base.stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closing, null);
                    this.closeAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false);
                    closeAsyncResult      = this.closeAsyncResult;
                    flag = true;
                }
                else if (((base.stateInfo.State == RunspacePoolState.Disconnected) || (base.stateInfo.State == RunspacePoolState.Disconnecting)) || (base.stateInfo.State == RunspacePoolState.Connecting))
                {
                    this.closeAsyncResult = new RunspacePoolAsyncResult(base.instanceId, callback, asyncState, false);
                    closeAsyncResult      = this.closeAsyncResult;
                }
                else if (base.stateInfo.State == RunspacePoolState.Closing)
                {
                    return(this.closeAsyncResult);
                }
            }
            if (flag)
            {
                base.RaiseStateChangeEvent(stateInfo);
            }
            if (!flag2)
            {
                this.dataStructureHandler.CloseRunspacePoolAsync();
                return(closeAsyncResult);
            }
            closeAsyncResult.SetAsCompleted(null);
            return(closeAsyncResult);
        }
        /// <summary>
        /// Closes the runspacepool synchronously / asynchronously. 
        /// </summary>
        /// <param name="isAsync">
        /// true to close asynchronously
        /// </param>
        /// <param name="callback">
        /// A AsyncCallback to call once the BeginClose completes.
        /// </param>
        /// <param name="asyncState">
        /// A user supplied state to call the <paramref name="callback"/>
        /// with.
        /// </param>
        /// <returns>
        /// asyncResult object to monitor status of the async 
        /// open operation. This is returned only if <paramref name="isAsync"/>
        /// is true.
        /// </returns>
        private IAsyncResult CoreClose(bool isAsync, AsyncCallback callback, object asyncState)
        {
            lock (syncObject)
            {
                if ((stateInfo.State == RunspacePoolState.Closed) ||
                    (stateInfo.State == RunspacePoolState.Broken) ||
                    (stateInfo.State == RunspacePoolState.Closing) ||
                    (stateInfo.State == RunspacePoolState.Disconnecting) ||
                    (stateInfo.State == RunspacePoolState.Disconnected))
                {
                    if (isAsync)
                    {
                        RunspacePoolAsyncResult asyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false);
                        asyncResult.SetAsCompleted(null);
                        return asyncResult;
                    }
                    else
                    {
                        return null;
                    }
                }

                stateInfo = new RunspacePoolStateInfo(RunspacePoolState.Closing, null);
            }

            // only one thread will reach here.
            RaiseStateChangeEvent(stateInfo);

            if (isAsync)
            {
                RunspacePoolAsyncResult asyncResult = new RunspacePoolAsyncResult(instanceId, callback, asyncState, false);
                //Open pool in another thread
                ThreadPool.QueueUserWorkItem(new WaitCallback(CloseThreadProc), asyncResult);
                return asyncResult;
            }

            // open the runspace synchronously
            CloseHelper();
            return null;
        }