This is all happening in a windows service.
I have a Queue
(actually a ConcurrentQueue
) holding items waiting to be processed
I'd also recommend using a BlockingCollection
instead of directly using a ConcurrentQueue
.
Here's an example:
public class QueuingRequestProcessor
{
private BlockingCollection queue;
public void QueuingRequestProcessor(int maxConcurrent)
{
this.queue = new BlockingCollection(maxConcurrent);
Task[] consumers = new Task[maxConcurrent];
for (int i = 0; i < maxConcurrent; i++)
{
consumers[i] = Task.Factory.StartNew(() =>
{
// Will wait when queue is empty, until CompleteAdding() is called
foreach (var request in this.queue.GetConsumingEnumerable())
{
Process(request);
}
});
}
}
public void Add(MyRequest request)
{
this.queue.Add(request);
}
public void Stop()
{
this.queue.CompleteAdding();
}
private void Process(MyRequestType request)
{
// Do your processing here
}
}
Note that maxConcurrent
in the constructor defines how many requests will be processed concurrently.