Reed is correct in telling you that you need to implement the IProducerConsumerCollection<T>
. However, there is a class that can help you. It's not built in, but it's featured on MSDN. Just pass this ConcurrentPriorityQueue
to your BlockingCollection
.
This is how I used it:
private readonly BlockingCollection<KeyValuePair<int, ICommand>> _commands
= new BlockingCollection<KeyValuePair<int, ICommand>>(
new ConcurrentPriorityQueue<int, ICommand>());
The ICommand
is an interface in my project.
Now this allows you to add items like this:
_actions.Add(new KeyValuePair<int, ICommand>(1, command1));
_actions.Add(new KeyValuePair<int, ICommand>(2, command2));
_actions.Add(new KeyValuePair<int, ICommand>(1, command3));
Items with a lower integer value as priority will be executed first. In the above example:
command1
command3
command2
When looping over your BlockingCollection
, you will no longer get the single elements (ICommand
in my case), but a KeyValuePair
. This might require some code changes of course. A nice thing is that you have its original priority:
foreach (var command in _queue)
{
var priority = command.Key;
var actualCommand = command.Value;
}