dotnet / dotnet/dotnet-api-docs

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

Open
#780 1 comment 0 reactions 0 assignees View on GitHub
.net/prod area-System.Threading.Tasks Pri2 unspecified/svc untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

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)](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1.getconsumingenumerable?view=netframework-4.7.2#System_Collections_Concurrent_BlockingCollection_1_GetConsumingEnumerable)
* Content Source: [xml/System.Collections.Concurrent/BlockingCollection`1.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Collections.Concurrent/BlockingCollection`1.xml)
* Service: **unspecified**
* Product: **.net**
* GitHub Login: @dotnet-bot
* Microsoft Alias: **dotnetcontent**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.