As the title suggests I utilize an abstract class to create a reusable base class for adding and removing items with a BlockingCollection. I use the producer / consumer pattern to enqueue and dequeue items, the abstract class is generic so I can specify an object for the queue.
My issue occurs when I call the base classes Enqueue and Dequeue methods, they seem to be referencing a different instance of the queue, ie when I call dequeue there are no items on the queue and the enqueues TryAdd() returns true. However when I directly call Add or Take on the queue instance it works fine.
Can anyone tell me why the bases classes Enqueue and Dequeue methods do not work as I expect, I have tried looking up several uses of BlockingCollections and cannot see why.
Program.cs
static void Main(string[] args)
{
var processor = new Processor(1,4);
Console.ReadKey();
}
Abstract Base class
public abstract class BaseProcessor<T>: IDisposable
{
protected BlockingCollection<T> _queue;
private CancellationTokenSource _tokenSource;
private int _producers;
private int _consumers;
private List<Task> _tasks;
public BaseProcessor(int producers, int consumers)
{
_queue = new BlockingCollection<T>();
_tokenSource = new CancellationTokenSource();
_producers = producers;
_consumers = consumers;
_tasks = new List<Task>();
}
protected void Setup()
{
Parallel.For(0, _producers, i =>
_tasks.Add(Task.Factory.StartNew(() => Produce(_tokenSource.Token), _tokenSource.Token)
.ContinueWith((task) =>
{
Console.WriteLine("Task {0} stopped", task.Id);
}))
);
Parallel.For(0, _producers, i =>
_tasks.Add(Task.Factory.StartNew(() => Consume(_tokenSource.Token), _tokenSource.Token)
.ContinueWith((task) =>
{
Console.WriteLine("Task {0} stopped", task.Id);
}))
);
}
protected abstract void Produce(CancellationToken token);
protected abstract void Consume(CancellationToken token);
protected void Enqueue(T item)
{
try
{
var res = _queue.TryAdd(item, TimeSpan.FromSeconds(1));
}
catch (Exception ex)
{
Console.WriteLine("Could not add item to queue: {0}", ex);
}
}
protected void Enqueue(List<T> items)
{
items.ForEach(o => Enqueue(o));
}
protected T Dequeue()
{
try
{
T item;
while (_queue.TryTake(out item, TimeSpan.FromSeconds(1))) ;
return item;
}
catch (Exception ex)
{
Console.WriteLine("Could not remove item to queue: {0}", ex);
return default(T);
}
}
public void Dispose()
{
// Cancel all tokens
_tokenSource.Cancel();
// Wait for all tasks complete
Task.WaitAll(_tasks.ToArray());
_queue.Dispose();
}
}
Abstract class implementation
public class Processor: BaseProcessor<QueueItem>
{
private int _counter;
public Processor(int producers, int consumers): base(producers, consumers)
{
_counter = 0;
Setup();
}
protected override void Produce(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var queueItem = new QueueItem()
{
Id = _counter,
Timestamp = DateTime.Now
};
//Enqueue(queueItem);
_queue.Add(queueItem);
Console.WriteLine("Enqueued: {0}", _counter);
_counter++;
Thread.Sleep(3000);
}
}
protected override void Consume(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var item = Dequeue();
//var item = _queue.Take();
Console.WriteLine("Dequeued: {0}", item.Id);
}
}
}
I have discovered the reason, its my implementation of some code taken from this link I was using. https://www.infoworld.com/article/3090215/how-to-work-with-blockingcollection-in-c.html
The while (_queue.TryTake(out item, TimeSpan.FromSeconds(1))); is dumping all the items from the queue, it is running until the queue is in fact empty. From the tutorial I presumed it was running whilst there was something to try and take.
Blind copy and paste issue on my behalf