Ejemplo n.º 1
0
        /// <summary>
        /// Enqueue a work item to the queue.
        /// </summary>
        public bool EnqueueWorkItem(WorkItem workItem)
        {
            // A work item cannot be null, since null is used in the WaitForWorkItem() method to indicate timeout or cancel
            if (workItem == null)
            {
                throw new ArgumentNullException("workItem");
            }

            bool enqueue = true;

            // First check if there is a wait entry waiting for work item. During
            // the check, timed out waiters are ignored. If there is no waiter then the work item is queued.
            lock (this)
            {
                CheckDisposed();

                if (!_isWorkItemsQueueActive)
                {
                    return(false);
                }

                while (_waitersCount > 0)
                {
                    // Dequeue a waiter.
                    WaitEntry waitEntry = PopWaiter();
                    // Signal the waiter. On success break the loop
                    if (waitEntry.Signal(workItem))
                    {
                        enqueue = false;
                        break;
                    }
                }

                if (enqueue)
                {
                    // Enqueue the work item
                    _workItems.Enqueue(workItem);
                }
            }
            return(true);
        }