Exemplo n.º 1
0
        public async Task <(T result, bool success)> TryDequeueAsync(CancellationToken token)
        {
            WaitQueueReader reader = null;
            Item            item   = new Item();

            lock (ThisLock)
            {
                if (_queueState == QueueState.Open)
                {
                    if (_itemQueue.HasAvailableItem)
                    {
                        item = _itemQueue.DequeueAvailableItem();
                    }
                    else
                    {
                        reader = new WaitQueueReader(this);
                        _readerQueue.Enqueue(reader);
                    }
                }
                else if (_queueState == QueueState.Shutdown)
                {
                    if (_itemQueue.HasAvailableItem)
                    {
                        item = _itemQueue.DequeueAvailableItem();
                    }
                    else if (_itemQueue.HasAnyItem)
                    {
                        reader = new WaitQueueReader(this);
                        _readerQueue.Enqueue(reader);
                    }
                    else
                    {
                        return(default(T), true);
                    }
                }
                else // queueState == QueueState.Closed
                {
                    return(default(T), true);
                }
            }

            if (reader != null)
            {
                return(await reader.WaitAsync(token));
            }
            else
            {
                InvokeDequeuedCallback(item.DequeuedCallback);
                return(item.GetValue(), true);
            }
        }