Processing only n items at a time concurrently using Task Parallel Library

前端 未结 3 673
醉话见心
醉话见心 2021-02-08 23:09

This is all happening in a windows service.

I have a Queue (actually a ConcurrentQueue) holding items waiting to be processed

3条回答
  •  Happy的楠姐
    2021-02-08 23:25

    Use BlockingCollection instead of ConcurrentQueue, then you can start any number of consumer threads and use Take method of the BlockingCollection. if the collection is empty, the Take method will automatically block in the caller thread waiting for items to be added, otherwise the threads will consume all the queue items in parallel. However as your question mentioned out the use of TPL it turns out that Parallel.ForEach have some issues when using with BlockingCollection check this post for more details. so you have to manage creation of your consumer threads your self. new Thread(/*consumer method*/) or new Task()...

提交回复
热议问题