Question

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?

Was it helpful?

Solution

Microsoft CCR contains much of what you need.

Here are some code samples and usage notes.

OTHER TIPS

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

Have you looked at MiscUtil ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top