Why does iterating over GetConsumingEnumerable() not fully empty the underlying blocking collection

前端 未结 4 1953
天涯浪人
天涯浪人 2021-02-04 04:16

I have a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection, ConcurrentQueue & GetCo

4条回答
  •  爱一瞬间的悲伤
    2021-02-04 04:49

    As of .net 4.5, you can create a partitioner which will take only 1 item at a time:

    var partitioner = Partitioner.Create(jobsBatchesQ.queue.GetConsumingEnumerable(), EnumerablePartitionerOptions.NoBuffering);
    Parallel.ForEach(partitioner, new ParallelOptions { MaxDegreeOfParallelism = (currentTask.ParallelLevel > 0 ? currentTask.ParallelLevel : 1) }, (batch, state) => {//do stuff}
    

    https://msdn.microsoft.com/en-us/library/system.collections.concurrent.enumerablepartitioneroptions(v=vs.110).aspx

提交回复
热议问题