Esempio n. 1
0
 /// <summary>
 /// Wait for the thread pool to be idle
 /// </summary>
 public override bool WaitForIdle(int millisecondsTimeout)
 {
     SmartThreadPool.ValidateWorkItemsGroupWaitForIdle(this);
     return(STPEventWaitHandle.WaitOne(_isIdleWaitHandle, millisecondsTimeout, false));
 }
Esempio n. 2
0
        /// <summary>
        /// Get the result of the work item.
        /// If the work item didn't run yet then the caller waits for the result, timeout, or cancel.
        /// In case of error the e argument is filled with the exception
        /// </summary>
        /// <returns>The result of the work item</returns>
        private object GetResult(
            int millisecondsTimeout,
            bool exitContext,
            WaitHandle cancelWaitHandle,
            out Exception e)
        {
            e = null;

            // Check for cancel
            if (WorkItemState.Canceled == GetWorkItemState())
            {
                throw new WorkItemCancelException("Work item canceled");
            }

            // Check for completion
            if (IsCompleted)
            {
                e = _exception;
                return(_result);
            }

            // If no cancelWaitHandle is provided
            if (null == cancelWaitHandle)
            {
                WaitHandle wh = GetWaitHandle();

                bool timeout = !STPEventWaitHandle.WaitOne(wh, millisecondsTimeout, exitContext);

                ReleaseWaitHandle();

                if (timeout)
                {
                    throw new WorkItemTimeoutException("Work item timeout");
                }
            }
            else
            {
                WaitHandle wh     = GetWaitHandle();
                int        result = STPEventWaitHandle.WaitAny(new WaitHandle[] { wh, cancelWaitHandle });
                ReleaseWaitHandle();

                switch (result)
                {
                case 0:
                    // The work item signaled
                    // Note that the signal could be also as a result of canceling the
                    // work item (not the get result)
                    break;

                case 1:
                case STPEventWaitHandle.WaitTimeout:
                    throw new WorkItemTimeoutException("Work item timeout");

                default:
                    Debug.Assert(false);
                    break;
                }
            }

            // Check for cancel
            if (WorkItemState.Canceled == GetWorkItemState())
            {
                throw new WorkItemCancelException("Work item canceled");
            }

            Debug.Assert(IsCompleted);

            e = _exception;

            // Return the result
            return(_result);
        }