dotnet / dotnet/docs

Setting max degree of concurrency for multiple tasks

Open
#31,135 0 comments 0 reactions 0 assignees View on GitHub
async-task-programming/subsvc dotnet-csharp/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

**Help us make content visible**

Hi,

Recently, there came a client's (or maybe devs' actually? lol) requirement:
- to process (thousands of) data records individually with a method which includes sending http request to an external web api for each individual item but don't start all processings at once and instead limit concurrency based on some variable in the configuration
.

That seemed fairly basic but I could not find any simple example of doing this in the [docs](https://docs.microsoft.com/en-us/dotnet/csharp/async) (I like particularly these ones as they do not use Task.Run / TaskFactory.StartNew and other advanced / uncommon System.Threading APIs).

**Describe the new article**

Eventually, we settled on a solution similar to this:
```cs
var data = Enumerable.Range(1, 27);
int maxDegreeOfConcurrency = 10;

var dataChunks = data.Chunk(maxDegreeOfConcurrency);

foreach (var dataChunk in dataChunks)
{
await Task.WhenAll(dataChunk.Select(dataItem => DoSomethingAsync(dataItem)));

Console.WriteLine();
await Task.Delay(4000);
}

async Task DoSomethingAsync(int i)
{
Console.WriteLine($"in: {i}");
await Task.Delay(1000);
Console.WriteLine($"out: {i}");
}
```
but I would like a simple, "recommended" way of solving the above problem somewhere in the docs.

Also, a basic article with guidance on Parallel.ForEachAsync vs Task.WhenAll would be appreciated.

---
#### Document Details

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

* ID: 07ed82a3-99c1-0893-4e81-d095b4757ecc
* Version Independent ID: 3765200b-43e5-441c-b290-709c34dd2f6d
* Content: [Asynchronous programming - C#](https://learn.microsoft.com/en-us/dotnet/csharp/async)
* Content Source: [docs/csharp/async.md](https://github.com/dotnet/docs/blob/main/docs/csharp/async.md)
* Product: **dotnet-csharp**
* Technology: **csharp-async**
* GitHub Login: @BillWagner
* Microsoft Alias: **wiwagn**

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.