I need single-producer, single-consumer FIFO query because
- I need to process messages in the order they received.
- I need to do this asynchronous because caller should not wait while I’m processing message.
- Next message processing should be started only when previous message processing is finished. Sometimes frequency of “receiving” messages is higher than frequency of “processing” messages. But in average I should be able to process all messages, just sometimes I have to “queue” pack of them.
So it’s pretty like TCP/IP I think, where you have one producer and one consumer, SOMETIMES you can receive messages faster than you can process, so you have to query them. Where order IS important and where caller absolutely not interested what you doing with that stuff.
This sounds easy enough and I likely can use general Queue for that, but I want to use BlockingCollection for that because I don’t want to write any code with ManualResetEvent etc.
How suitable BlockingCollection for my task and probably you can suggest something else?
BlockingCollectionclass implements IProducerConsumerCollection interface so perfectly fits your requirements.You can create two Tasks, one for async producer and an other one as consumer worker. Former would add items to
BlockingCollectionand the latter just consume as soon as new are available in FIFO order.Producer-consumer sample application using TPL Tasks and BlockingCollection:
IProducerConsumerCollection: