Questions tagged [parallel.foreachasync]

Parallel.ForEachAsync is a .NET method for processing an enumerable sequence in parallel, using an asynchronous delegate.

Parallel.ForEachAsync is a .NET method for processing an enumerable sequence in parallel, using an asynchronous delegate. Both synchronous and asynchronous source sequences are supported. The asynchronous delegate accepts a CancellationToken and returns a ValueTask. This method it is part of the .

27 questions
14
votes
2 answers

Stop Parallel.ForEachAsync

In C#, I am interested in stopping a Parallel.ForEachAsync loop (considering the differences between Stop and Break); for Parallel.ForEach I can do the following: Parallel.ForEach(items, (item, state) => { if…
13
votes
1 answer

Using Parallel.ForEachAsync

I'm trying to run a Parallel.ForEachAsync(), but I am getting these two errors: Error 1: Argument 2: can not convert from System.Threading.Tasks.ParallelOptions to System.Threading.CancellationToken Error 2: Delegate Func
8
votes
3 answers

Factors for determining the degree of parallelism for the ForEachAsync

Below is an implementation of ForEachAsync written by Stephen Toub. public static Task ForEachAsync(this IEnumerable source, int dop, Func body) { return Task.WhenAll( from partition in…
6
votes
1 answer

Is Parallel.ForEachAsync a replacement to a plain for loop + append to task list (async await Task.Run)+ WhenAll?

Say I want to make parallel API post requests. In a for loop I can append the http post call into a list of tasks, (each task invoked using Task.Run) and then wait for all to finish using await Task.WhenAll. Thus the control will go to caller while…
variable
  • 8,262
  • 9
  • 95
  • 215
6
votes
1 answer

What do I specify as the Dop parameter for ForEachAsync extension method?

I recently discovered the following code below to effectively run lots of I/O bound tasks: Implementing a simple ForEachAsync, part 2 I'm under the impression the following are true: This is much better than using Parallel.ForEach because the work…
4
votes
1 answer

How to break the Parallel.ForEachAsync loop, not cancel it?

In .NET 5 we had Parallel.ForEach which you were able to use ParallelLoopState.Break() method to stop additional iterations from processing. Allowing current ones to complete processing. But the new .NET 6 Parallel.ForEachAsync does not have the…
stymie2
  • 101
  • 10
4
votes
2 answers

The need for two cancellation tokens in .NET 6 Parallel.ForEachAsync?

I was experimenting with how to break out of a ForEachAsync loop. break doesn't work, but I can call Cancel on the CancellationTokenSource. The signature for ForEachAsync has two tokens - one as a stand-alone argument and one in the Func body…
Vic F
  • 1,143
  • 1
  • 11
  • 26
3
votes
4 answers

From IEnumerable> to IAsyncEnumerable by yield returning inside a Parallel.ForEach/Parallel.ForEachAsync gives error CS1621

In a .NET 6 project, I have to call a web API which is offset paginated (page/per page) and I would like to make the n calls parallel as far as possible. This is the method which calls the API one time with the given page number: private…
2
votes
2 answers

Actual maximum concurrent tasks of Parallel.ForEachAsync

I would expect this code to take 1 second to execute: public async void Test() { DateTime start = DateTime.Now; await Parallel.ForEachAsync(new int[1000], new ParallelOptions { MaxDegreeOfParallelism = 1000 }, async (i, token) => { …
2
votes
0 answers

Why Does Parallel.ForEachAsync Use All Threads On My Box Waiting Using AsyncManualResetEvent?

What I'm trying to accomplish is to get a particular record before I process the rest. Unfortunately, I don't have the freedom to enumerate twice to get the 1 record I need before processing the rest. I do know that the record I'm looking for will…
2
votes
1 answer

What is the meaning of the MaxDegreeOfParallelism = -1 in Parallel operations in .NET 6?

The documentation of the ParallelOptions.MaxDegreeOfParallelism property states that: The MaxDegreeOfParallelism property affects the number of concurrent operations run by Parallel method calls that are passed this ParallelOptions instance. A…
2
votes
3 answers

Parallel.ForEachAsync is not waiting for all tasks

Below is sample console app and output is Output is different each time and is fine but it needs to complete all tasks before I print result. It seems that Parallel.ForEachAsync is not waiting for all tasks to be completed. Am I missing anything…
2
votes
1 answer

Is it possible to throttle Parallel.ForEachAsync in .NET 6.0 to avoid rate limiting?

I'm fairly new to programming (< 3 years exp), so I don't have a great understanding of the subjects in this post. Please bear with me. My team is developing an integration with a third party system, and one of the third party's endpoints lacks a…
1
vote
1 answer

How to return inside a ValueTask returning lambda of Parallel.ForEachAsync?

In general we can await an async method invocation in any one of the Branch of execution. And other branches we can simply return. This will not give any warning. But when we do the same in Parallel.ForEachAsync, We get the warning CS4014 "Because…
Siva Sankaran
  • 1,521
  • 4
  • 21
  • 40
1
vote
1 answer

How to run a Parallel.ForEachAsync loop with NoBuffering?

The synchronous Parallel.ForEach method has many overloads, and some of them allow to configure the parallel loop with the EnumerablePartitionerOptions.NoBuffering option: Create a partitioner that takes items from the source enumerable one at a…
1
2