Generic .Net Producer/Consumer

余生长醉 提交于 2019-12-04 10:52:18

问题


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?


回答1:


Microsoft CCR contains much of what you need.

Here are some code samples and usage notes.




回答2:


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




回答3:


Have you looked at MiscUtil ?



来源:https://stackoverflow.com/questions/916863/generic-net-producer-consumer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!