Ejemplo n.º 1
0
        protected virtual void OnQueueFull(EventWaitHandle waitHandle)
        {
            if (QueueFull != null && _size > 0)
            {
                T[] array = new T[_size];

                int i = 0, j = 0, num = ((_tail < _head) ? _array.Length : _tail + 1) - _head;
                while (num-- > 0)
                {
                    j          = _head + i;
                    array[i++] = _array[j];
                    _array[j]  = null;
                }

                // If the head is above the tail we need to get the
                // elements that exists between the top of the queue and
                // the the tail.
                if (_tail < _head)
                {
                    // when this method is called the tail points
                    // to the last used slot and not to the next free slot.
                    // So we need to include clean that node too.
                    for (j = 0; j <= _tail; j++)
                    {
                        array[i++] = _array[j];
                        _array[j]  = null;
                    }
                }

                _tail = 0;
                _head = 0;
                _size = 0;

                // the event delegate will be executed in another thread
                EventState state = new EventState(QueueFull, array, waitHandle);
                ThreadPool.QueueUserWorkItem(delegate(object o)
                {
                    EventState e = (EventState)o;
                    e.handler(e.array);
                    if (e.waithandle != null)
                    {
                        e.waithandle.Set();
                    }
                }, state);
            }
            else
            {
                waitHandle.Set();
            }
        }