internal IAsyncResult BeginGetRunspace(AsyncCallback callback, object state)
        {
            this.AssertPoolIsOpen();
            GetRunspaceAsyncResult requestToEnqueue = new GetRunspaceAsyncResult(this.InstanceId, callback, state);

            this.EnqueueCheckAndStartRequestServicingThread(requestToEnqueue, true);
            return(requestToEnqueue);
        }
Beispiel #2
0
 public Runspace GetRunspace()
 {
     using (RunspacePoolInternal.tracer.TraceMethod())
     {
         this.AssertPoolIsOpen();
         GetRunspaceAsyncResult runspace = (GetRunspaceAsyncResult)this.BeginGetRunspace((AsyncCallback)null, (object)null);
         runspace.AsyncWaitHandle.WaitOne();
         return(runspace.Exception == null ? runspace.Runspace : throw runspace.Exception);
     }
 }
        public Runspace GetRunspace()
        {
            this.AssertPoolIsOpen();
            GetRunspaceAsyncResult result = (GetRunspaceAsyncResult)this.BeginGetRunspace(null, null);

            result.AsyncWaitHandle.WaitOne();
            if (result.Exception != null)
            {
                throw result.Exception;
            }
            return(result.Runspace);
        }
        internal void CancelGetRunspace(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw PSTraceSource.NewArgumentNullException("asyncResult");
            }
            GetRunspaceAsyncResult result = asyncResult as GetRunspaceAsyncResult;

            if ((result == null) || (result.OwnerId != this.instanceId))
            {
                throw PSTraceSource.NewArgumentException("asyncResult", resBaseName, "AsyncResultNotOwned", new object[] { "IAsyncResult", "BeginGetRunspace" });
            }
            result.IsActive = false;
        }
        internal Runspace EndGetRunspace(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw PSTraceSource.NewArgumentNullException("asyncResult");
            }
            GetRunspaceAsyncResult result = asyncResult as GetRunspaceAsyncResult;

            if ((result == null) || (result.OwnerId != this.instanceId))
            {
                throw PSTraceSource.NewArgumentException("asyncResult", resBaseName, "AsyncResultNotOwned", new object[] { "IAsyncResult", "BeginGetRunspace" });
            }
            result.EndInvoke();
            return(result.Runspace);
        }
        protected void EnqueueCheckAndStartRequestServicingThread(GetRunspaceAsyncResult requestToEnqueue, bool useCallingThread)
        {
            bool flag = false;

            lock (this.runspaceRequestQueue)
            {
                if (requestToEnqueue != null)
                {
                    this.runspaceRequestQueue.Enqueue(requestToEnqueue);
                }
                if (this.isServicingRequests)
                {
                    return;
                }
                if ((this.runspaceRequestQueue.Count + this.ultimateRequestQueue.Count) > 0)
                {
                    lock (this.pool)
                    {
                        if ((this.pool.Count > 0) || (this.totalRunspaces < this.maxPoolSz))
                        {
                            this.isServicingRequests = true;
                            if (useCallingThread && (this.ultimateRequestQueue.Count == 0))
                            {
                                flag = true;
                            }
                            else
                            {
                                ThreadPool.QueueUserWorkItem(new WaitCallback(this.ServicePendingRequests), false);
                            }
                        }
                    }
                }
            }
            if (flag)
            {
                this.ServicePendingRequests(true);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Begin for obtaining a runspace for the specified ConnectionInfo
        /// </summary>
        /// <param name="connectionInfo">connection info to be used for remote connections</param>
        /// <param name="retryCount">number of times to retry</param>
        /// <param name="callback">optional user defined callback</param>
        /// <param name="state">optional user specified state</param>
        /// <param name="retryInterval">time in milliseconds before the next retry has to be attempted</param>
        /// <returns>async result</returns>
        public override IAsyncResult BeginGetRunspace(WSManConnectionInfo connectionInfo, uint retryCount, uint retryInterval, AsyncCallback callback, object state)
        {
            GetRunspaceAsyncResult result = new GetRunspaceAsyncResult(state, callback, Guid.Empty);

            // Create a Request Object and queue the same
            RequestInfo requestInfo = new RequestInfo
                                          {
                                              ConnectionInfo = connectionInfo,
                                              RetryCount = retryCount,
                                              AsyncResult = result,
                                              RetryInterval = retryInterval
                                          };

            _tracer.WriteMessage("PSW ConnMgr: New incoming request for runspace queued");
            _inComingRequests.Enqueue(requestInfo);
            _perfCountersMgr.UpdateCounterByValue(
                    PSWorkflowPerformanceCounterSetInfo.CounterSetId,
                    PSWorkflowPerformanceCounterIds.PSRemotingPendingRequestsQueueLength);

            // start servicing thread if required
            CheckAndStartRequiredThreads();

            return result;
        }
Beispiel #8
0
 internal void AddToPendingCallback(GetRunspaceAsyncResult asyncResult)
 {
     _callbacks.Enqueue(asyncResult);
     CheckAndStartCallbackServicingThread();
 }
        /// <summary>
        /// Gets Runspace asynchronously from the runspace pool. The caller
        /// will get notified with the runspace using <paramref name="callback"/>
        /// </summary>
        /// <param name="callback">
        /// A AsyncCallback to call once the runspace is available.
        /// </param>
        /// <param name="state">
        /// A user supplied state to call the <paramref name="callback"/>
        /// with.
        /// </param>
        /// <returns>
        /// An IAsyncResult object to track the status of the Async operation.
        /// </returns>
        internal IAsyncResult BeginGetRunspace(
            AsyncCallback callback, object state)
        {
            AssertPoolIsOpen();

            GetRunspaceAsyncResult asyncResult = new GetRunspaceAsyncResult(this.InstanceId,
                callback, state);

            // Enqueue and start servicing thread in one go..saving multiple locks.
            EnqueueCheckAndStartRequestServicingThread(asyncResult, true);

            return asyncResult;
        }
        /// <summary>
        /// If <paramref name="requestToEnqueue"/> is not null, enqueues the request.
        /// Checks if a thread pool thread is queued to service pending requests
        /// for runspace. If a thread is not queued, queues one.
        /// </summary>
        /// <param name="requestToEnqueue">
        /// Used by calling threads to queue a request before checking and starting
        /// servicing thread.
        /// </param>
        /// <param name="useCallingThread">
        /// uses calling thread to assign available runspaces (if any) to runspace
        /// requesters.
        /// </param>
        protected void EnqueueCheckAndStartRequestServicingThread(GetRunspaceAsyncResult requestToEnqueue,
            bool useCallingThread)
        {
            bool shouldStartServicingInSameThread = false;
            lock (runspaceRequestQueue)
            {
                if (requestToEnqueue != null)
                {
                    runspaceRequestQueue.Enqueue(requestToEnqueue);
                }

                // if a thread is already servicing requests..just return.
                if (isServicingRequests)
                {
                    return;
                }

                if ((runspaceRequestQueue.Count + ultimateRequestQueue.Count) > 0)
                {
                    // we have requests pending..check if a runspace is available to
                    // service the requests.
                    lock (pool)
                    {
                        if ((pool.Count > 0) || (totalRunspaces < maxPoolSz))
                        {
                            isServicingRequests = true;
                            if ((useCallingThread) && (ultimateRequestQueue.Count == 0))
                            {
                                shouldStartServicingInSameThread = true;
                            }
                            else
                            {
                                // release a async result object using a thread pool thread.
                                // this way the calling thread will not block.
                                ThreadPool.QueueUserWorkItem(new WaitCallback(ServicePendingRequests), false);
                            }
                        }
                    }
                }
            }

            // only one thread will be here if any..
            // This will allow us to release lock.
            if (shouldStartServicingInSameThread)
            {
                ServicePendingRequests(true);
            }
        }
        protected void ServicePendingRequests(object useCallingThreadState)
        {
            if ((this.stateInfo.State == RunspacePoolState.Closed) || (this.stateInfo.State == RunspacePoolState.Closing))
            {
                return;
            }
            bool flag = (bool)useCallingThreadState;
            GetRunspaceAsyncResult result = null;

            try
            {
                bool flag4;
                Queue <GetRunspaceAsyncResult> queue = null;
Label_0026:
                flag4 = false;
                try
                {
                    Monitor.Enter(queue = this.ultimateRequestQueue, ref flag4);
                    while (this.ultimateRequestQueue.Count > 0)
                    {
                        Runspace runspace;
                        if (this.stateInfo.State == RunspacePoolState.Closing)
                        {
                            return;
                        }
                        lock (this.pool)
                        {
                            if (this.pool.Count > 0)
                            {
                                runspace = this.pool.Pop();
                            }
                            else
                            {
                                if (this.totalRunspaces >= this.maxPoolSz)
                                {
                                    return;
                                }
                                runspace = this.CreateRunspace();
                            }
                        }
                        result = this.ultimateRequestQueue.Dequeue();
                        if (!result.IsActive)
                        {
                            lock (this.pool)
                            {
                                this.pool.Push(runspace);
                            }
                            result.Release();
                        }
                        else
                        {
                            result.Runspace = runspace;
                            if (flag)
                            {
                                goto Label_01B9;
                            }
                            ThreadPool.QueueUserWorkItem(new WaitCallback(result.DoComplete));
                        }
                    }
                }
                finally
                {
                    if (flag4)
                    {
                        Monitor.Exit(queue);
                    }
                }
                lock (this.runspaceRequestQueue)
                {
                    if (this.runspaceRequestQueue.Count != 0)
                    {
                        goto Label_0167;
                    }
                    goto Label_01B9;
Label_0151:
                    this.ultimateRequestQueue.Enqueue(this.runspaceRequestQueue.Dequeue());
Label_0167:
                    if (this.runspaceRequestQueue.Count > 0)
                    {
                        goto Label_0151;
                    }
                    goto Label_0026;
                }
            }
            finally
            {
                lock (this.runspaceRequestQueue)
                {
                    this.isServicingRequests = false;
                    this.EnqueueCheckAndStartRequestServicingThread(null, false);
                }
            }
Label_01B9:
            if (flag && (result != null))
            {
                result.DoComplete(null);
            }
        }
Beispiel #12
0
		public override IAsyncResult BeginGetRunspace(WSManConnectionInfo connectionInfo, uint retryCount, uint retryInterval, AsyncCallback callback, object state)
		{
			GetRunspaceAsyncResult getRunspaceAsyncResult = new GetRunspaceAsyncResult(state, callback, Guid.Empty);
			ConnectionManager.RequestInfo requestInfo = new ConnectionManager.RequestInfo();
			requestInfo.ConnectionInfo = connectionInfo;
			requestInfo.RetryCount = retryCount;
			requestInfo.AsyncResult = getRunspaceAsyncResult;
			requestInfo.RetryInterval = retryInterval;
			ConnectionManager.RequestInfo requestInfo1 = requestInfo;
			this._tracer.WriteMessage("PSW ConnMgr: New incoming request for runspace queued");
			this._inComingRequests.Enqueue(requestInfo1);
			ConnectionManager._perfCountersMgr.UpdateCounterByValue(PSWorkflowPerformanceCounterSetInfo.CounterSetId, 24, (long)1, true);
			this.CheckAndStartRequiredThreads();
			return getRunspaceAsyncResult;
		}