Skip to content

https://gist.github.com/sheastrickland/49392be4bb7b2f6b2143 #780

@mjdaly

Description

@mjdaly

The examples do not document one of the most useful combinations, BlockingCollection and Parallel.ForEach.

There is one undocumented requirement for these two constructs to function well together. Both GetConsumingEnumerable and Parallel.ForEach contain chunking logic, which can prevent items from being processed until CompleteAdding is called.

A Partitioner need to be used with NoBuffering.

var options = new ParallelOptions { MaxDegreeOfParallelism = 4 };
var partitioner = Partitioner.Create(blockingCollection.GetConsumingEnumerable(), EnumerablePartitionerOptions.NoBuffering);
var results = Parallel.ForEach(partitioner, options, item => { /* PROCESS ITEM*/ });

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

  • ID: f8df56b2-d5da-8d38-ea9d-7f2fb50f877f
  • Version Independent ID: f903165e-d4c7-939f-6679-413310730fe6
  • Content: BlockingCollection.GetConsumingEnumerable Method (System.Collections.Concurrent)
  • Content Source: [xml/System.Collections.Concurrent/BlockingCollection1.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Collections.Concurrent/BlockingCollection1.xml)
  • Service: unspecified
  • Product: .net
  • GitHub Login: @dotnet-bot
  • Microsoft Alias: dotnetcontent

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions