views:

662

answers:

3

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?

A: 

Have you looked at MiscUtil ?

Ruben Bartelink
I don't see anything in there that does what I'm asking for.
Joel Coehoorn
Not to say I didn't see some things in there that would be very useful in implementing this, though.
Joel Coehoorn
So you'll be putting any enhancments you do inyour impl up there in time then... :P
Ruben Bartelink
Interesting link
Goblyn27
+2  A: 

Microsoft CCR contains much of what you need.

Here are some code samples and usage notes.

ripper234
I _knew_ this had to be out there somewhere already.
Joel Coehoorn
It is not a perfect framework though, lacking some obvious stuff like the ability to STOP the processing and WAIT for remaining tasks.We are enhancing the framework in our dev team, if you need more help let me know...
ripper234
+1  A: 

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

Don Kirkby