Beispiel #1
0
        private void Worker(object obj)
        {
            while (!_running)
            {
                _resetEvent.WaitOne();

                ProducerConsumerItem <T> consumerProducerItem = null;

                lock (_lock)
                {
                    if (_items.Count > 0)
                    {
                        consumerProducerItem = _items.Dequeue();
                    }

                    if (_items.Count == 0)
                    {
                        _resetEvent.Reset();
                    }
                }

                if (consumerProducerItem != null)
                {
                    consumerProducerItem.Set();
                    OnItemProcessed(consumerProducerItem.Item);
                }
            }
        }
Beispiel #2
0
        public void Add(T item, bool wait = false)
        {
            ProducerConsumerItem <T> consumerProducerItem = new ProducerConsumerItem <T>(item);

            lock (_lock)
            {
                _items.Enqueue(consumerProducerItem);

                OnItemAdded(item);

                _resetEvent.Set();
            }

            if (wait)
            {
                consumerProducerItem.WaitOne();
            }
        }