From e03af3f183ef1e1f433ea95fbac7f8c926d65d6d Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Tue, 9 Sep 2025 00:00:21 +0200 Subject: [PATCH 1/4] Show the example of simplified approach with Task.WhenEach --- ...tasks-and-process-them-as-they-complete.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md index 848aeb96cd701..a399ae7750329 100644 --- a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md +++ b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md @@ -166,6 +166,29 @@ For any given URL, the method will use the `client` instance provided to get the Run the program several times to verify that the downloaded lengths don't always appear in the same order. +## Simplify the approach using `Task.WhenEach` + +The `while` loop implemented in `SumPageSizesAsync` method can be simplified using the new method introduced in .NET 9, by calling it in `await foreach` loop. +
Replace the previously implemented `while` loop: + +```csharp + while (downloadTasks.Any()) + { + Task finishedTask = await Task.WhenAny(downloadTasks); + downloadTasks.Remove(finishedTask); + total += await finishedTask; + } +``` + +with the simplified `await foreach`: + +```csharp + await foreach (Task t in Task.WhenEach(downloadTasks)) + { + total += await t; + } +``` + > [!CAUTION] > You can use `WhenAny` in a loop, as described in the example, to solve problems that involve a small number of tasks. However, other approaches are more efficient if you have a large number of tasks to process. For more information and examples, see [Processing tasks as they complete](https://devblogs.microsoft.com/pfxteam/processing-tasks-as-they-complete). From e4fc49ca3dfdee797daa352b685b9cce2d047663 Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Tue, 9 Sep 2025 00:09:06 +0200 Subject: [PATCH 2/4] Move the new section below the CAUTION note --- ...ultiple-async-tasks-and-process-them-as-they-complete.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md index a399ae7750329..fb224e53b33d8 100644 --- a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md +++ b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md @@ -166,6 +166,9 @@ For any given URL, the method will use the `client` instance provided to get the Run the program several times to verify that the downloaded lengths don't always appear in the same order. +> [!CAUTION] +> You can use `WhenAny` in a loop, as described in the example, to solve problems that involve a small number of tasks. However, other approaches are more efficient if you have a large number of tasks to process. For more information and examples, see [Processing tasks as they complete](https://devblogs.microsoft.com/pfxteam/processing-tasks-as-they-complete). + ## Simplify the approach using `Task.WhenEach` The `while` loop implemented in `SumPageSizesAsync` method can be simplified using the new method introduced in .NET 9, by calling it in `await foreach` loop. @@ -189,9 +192,6 @@ with the simplified `await foreach`: } ``` -> [!CAUTION] -> You can use `WhenAny` in a loop, as described in the example, to solve problems that involve a small number of tasks. However, other approaches are more efficient if you have a large number of tasks to process. For more information and examples, see [Processing tasks as they complete](https://devblogs.microsoft.com/pfxteam/processing-tasks-as-they-complete). - ## Complete example The following code is the complete text of the *Program.cs* file for the example. From fa8ce6292888d5c9a9b1702694a03c119a3f12ec Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Tue, 9 Sep 2025 00:13:40 +0200 Subject: [PATCH 3/4] Briefly describe differences between two approaches --- ...rt-multiple-async-tasks-and-process-them-as-they-complete.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md index fb224e53b33d8..c422b0aa92e29 100644 --- a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md +++ b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md @@ -192,6 +192,8 @@ with the simplified `await foreach`: } ``` +This new approach allows to no longer repeatedly call `Task.WhenAny` to manually call a task and remove the one that finishes, because `Task.WhenEach` iterates through task *in an order of their completion*. + ## Complete example The following code is the complete text of the *Program.cs* file for the example. From cee4bc4f4a8af7d2304d51104cec7dd88091fb23 Mon Sep 17 00:00:00 2001 From: Bartosz Klonowski Date: Tue, 9 Sep 2025 00:14:43 +0200 Subject: [PATCH 4/4] Add Task.WhenEach to See also section --- ...art-multiple-async-tasks-and-process-them-as-they-complete.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md index c422b0aa92e29..40189bba94946 100644 --- a/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md +++ b/docs/csharp/asynchronous-programming/start-multiple-async-tasks-and-process-them-as-they-complete.md @@ -203,4 +203,5 @@ The following code is the complete text of the *Program.cs* file for the example ## See also - +- - [Asynchronous programming with async and await (C#)](index.md)