Beispiel #1
0
        public IAsyncResult BeginDequeue(TimeSpan timeout, AsyncCallback callback, object state)
        {
            Item <T> item = new Item <T>();

            lock (this.ThisLock)
            {
                if (this.queueState == QueueState <T> .Open)
                {
                    if (!this.itemQueue.HasAvailableItem)
                    {
                        AsyncQueueReader <T> reader = new AsyncQueueReader <T>((InputQueue <T>) this, timeout, callback, state);
                        this.readerQueue.Enqueue(reader);
                        return(reader);
                    }
                    item = this.itemQueue.DequeueAvailableItem();
                }
                else if (this.queueState == QueueState <T> .Shutdown)
                {
                    if (this.itemQueue.HasAvailableItem)
                    {
                        item = this.itemQueue.DequeueAvailableItem();
                    }
                    else if (this.itemQueue.HasAnyItem)
                    {
                        AsyncQueueReader <T> reader2 = new AsyncQueueReader <T>((InputQueue <T>) this, timeout, callback, state);
                        this.readerQueue.Enqueue(reader2);
                        return(reader2);
                    }
                }
            }
            InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback);

            return(new CompletedAsyncResult <T>(item.GetValue(), callback, state));
        }
Beispiel #2
0
            static void TimerCallback(object state)
            {
                AsyncQueueReader thisPtr = (AsyncQueueReader)state;

                if (thisPtr.inputQueue.RemoveReader(thisPtr))
                {
                    thisPtr.expired = true;
                    thisPtr.Complete(false);
                }
            }
            static void CancelCallback(object state)
            {
                AsyncQueueReader thisPtr = (AsyncQueueReader)state;

                thisPtr.cancelRegistration.Dispose();
                if (thisPtr.inputQueue.RemoveReader(thisPtr))
                {
                    thisPtr.TrySetCanceled();
                }
            }
Beispiel #4
0
        public bool EndDequeue(IAsyncResult result, out T value)
        {
            if (result is CompletedAsyncResult <T> )
            {
                value = CompletedAsyncResult <T> .End(result);

                return(true);
            }
            return(AsyncQueueReader <T> .End(result, out value));
        }
            public static bool End(IAsyncResult result, out T value)
            {
                AsyncQueueReader readerResult = End <AsyncQueueReader>(result);

                if (readerResult.expired)
                {
                    value = default(T);
                    return(false);
                }
                value = readerResult.item;
                return(true);
            }
Beispiel #6
0
        public bool EndDequeue(IAsyncResult result, out T value)
        {
            CompletedAsyncResult <T> typedResult = result as CompletedAsyncResult <T>;

            if (typedResult != null)
            {
                value = CompletedAsyncResult <T> .End(result);

                return(true);
            }

            return(AsyncQueueReader.End(result, out value));
        }
Beispiel #7
0
        public IAsyncResult BeginDequeue(TimeSpan timeout, AsyncCallback callback, object state)
        {
            Item item = default(Item);

            lock (ThisLock)
            {
                if (queueState == QueueState.Open)
                {
                    if (itemQueue.HasAvailableItem)
                    {
                        item = itemQueue.DequeueAvailableItem();
                    }
                    else
                    {
                        AsyncQueueReader reader = new AsyncQueueReader(this, timeout, callback, state);
                        readerQueue.Enqueue(reader);
                        return(reader);
                    }
                }
                else if (queueState == QueueState.Shutdown)
                {
                    if (itemQueue.HasAvailableItem)
                    {
                        item = itemQueue.DequeueAvailableItem();
                    }
                    else if (itemQueue.HasAnyItem)
                    {
                        AsyncQueueReader reader = new AsyncQueueReader(this, timeout, callback, state);
                        readerQueue.Enqueue(reader);
                        return(reader);
                    }
                }
            }

            InvokeDequeuedCallback(item.DequeuedCallback);
            return(new CompletedAsyncResult <T>(item.GetValue(), callback, state));
        }
        public Task <T> DequeueAsync(CancellationToken cancellationToken, object state)
        {
            Item item = default(Item);

            lock (ThisLock)
            {
                if (queueState == QueueState.Open)
                {
                    if (itemQueue.HasAvailableItem)
                    {
                        item = itemQueue.DequeueAvailableItem();
                    }
                    else
                    {
                        var reader = new AsyncQueueReader(this, cancellationToken, state);
                        readerQueue.Enqueue(reader);
                        return(reader.Task);
                    }
                }
                else if (queueState == QueueState.Shutdown)
                {
                    if (itemQueue.HasAvailableItem)
                    {
                        item = itemQueue.DequeueAvailableItem();
                    }
                    else if (itemQueue.HasAnyItem)
                    {
                        AsyncQueueReader reader = new AsyncQueueReader(this, cancellationToken, state);
                        readerQueue.Enqueue(reader);
                        return(reader.Task);
                    }
                }
            }

            InvokeDequeuedCallback(item.DequeuedCallback);
            return(Task.FromResult(item.GetValue()));
        }