Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic .Net Producer/Consumer

I'm toying with the idea of implementing a generic Producer/Consumer pair + processing queue in C# for fun. The idea is you can just create objects that implement appropriate IProducer and IConsumer interfaces (default implementations supplied), which will consist mainly of delegates, pass them to a QueueProcessor class instance, tell it how many consumers you want, and go.

But I say to myself, "Self, surely this has been done before."

So does anyone know of a good, generic implementation of the producer/consumer pattern in C# (VB.Net is okay, too)? The basic requirements I'm looking for:

  • Use generics for the produced and consumed types (input, queued task, and output types, or any combination thereof)
  • Allow you specify how many consumers will work the queue
  • Allow you to link up or chain multiple queues into a pipeline (tricky with multiple Consumers, I know)
  • Allow you to implement your own Producers and Consumers
  • Allow you to turn any IEnumerable into a producer (okay if I have to implement that myself, as long it's possible)
  • Delegate based (you can use lambda syntax for the basic consumer or producer work to process a single item)

Or if there is none, what pitfalls have prevented it and do you have any thoughts on how to implement it?

like image 434
Joel Coehoorn Avatar asked May 27 '09 16:05

Joel Coehoorn


2 Answers

Microsoft CCR contains much of what you need.

Here are some code samples and usage notes.

like image 160
ripper234 Avatar answered Nov 14 '22 05:11

ripper234


Marc Gravell wrote a nice example blocking queue in this answer.

like image 21
Don Kirkby Avatar answered Nov 14 '22 05:11

Don Kirkby