BlockingCollection
I use these all the time, but most recently I built it in to a metric collection service where I wanted to capture the data, but not crush my system by recording it.
Adding Items
Adding items to a BlockingCollection is just as easy as any other collection:
Processing Items
To process a queue, put it in a loop and take items from it. You can kick off this process in its own thread by using Task.Run:
You can Take (de-queue) items from a BlockingCollection in a couple of different ways:
TryTake
The first parameter is an out parameter, assigning the value taken out of the BlockingCollection
The second parameter is a timeout – This will cause TryTake to return false if the thread waits longer than the timeout to retrieve an item. This can happen if the collection is empty, so if you’re using the BlockingCollection as a consumer queue, it might make sense to set this time Timeout.Infinite
The third parameter is a CancellationToken. Canceling this CancellationToken will short-circuit the TryTake, but it will also throw a OperationCanceledException, so you probably want to handle that.
Take
Take doesn’t have any concept of a timeout, but you can still provide a CancellationToken to be observed to short-circuit the process if necessary.