Exemple #1
0
        /// <summary>
        /// Metodo bool Put(T t, int timeout), que insere t na fila, bloquando-se no maximo t milisegundos caso
        /// não exista espaco disponvel
        /// </summary>
        /// <param name="t"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public bool Put(T t, int timeout)
        {
            lock (this)
            {
                if (_closing || _closed)
                {
                    throw new InvalidOperationException("Queue is no longuer acepting items");
                }
                int lastTime = timeout == Timeout.Infinite ? 0 : Environment.TickCount;
                while (_queue.Count == MAX_SIZE)
                {
                    Monitor.Wait(this, timeout);
                    if (SyncUtils.AdjustTimeout(lastTime, ref timeout) == 0)
                    {
                        return(false);
                    }
                }
                if (_closing || _closed)
                {
                    throw new InvalidOperationException("Queue is no longuer acepting items");
                }
                _queue.Enqueue(t);
#if DEBUG
                Console.WriteLine("enqueued [{0}] queue size={1}", t, _queue.Count());
#endif
                Monitor.PulseAll(this);
                return(true);
            }
        }
Exemple #2
0
        /// <summary>
        /// Metodo T Get(int timetout), que retira um elemento da fila, bloqueando-se no maximo t milisegundos
        /// caso esse elemento não esteja imediatamente disponvel
        /// </summary>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public T Get(int timeout)
        {
            lock (this)
            {
                int lastTime = timeout == Timeout.Infinite ? 0 : Environment.TickCount;
                while (_queue.Count == 0)
                {
                    Monitor.Wait(this, timeout);

                    if (SyncUtils.AdjustTimeout(lastTime, ref timeout) == 0)
                    {
                        throw new TimeoutException();
                    }
                }
                T t = _queue.Dequeue();
#if DEBUG
                Console.WriteLine("dequeued [{0}] queue size={1}", t, _queue.Count());
#endif
                Monitor.PulseAll(this);
                return(t);
            }
        }