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 ParallelLoopState
class so we can't break it like we could with Parallel.ForEach
. So is there a way to perform the same break functionality in ForEachAsync
? CancellationToken
passed to the func I don't believe is the right way since your not trying to cancel the running loop but preventing additional iterations from starting.
Something like this functionality but for the async version:
int count = 0;
Parallel.ForEach(enumerateFiles, new ParallelOptions() { CancellationToken = cancellationToken},
(file, state) =>
{
Interlocked.Increment(ref count);
if (count >= MaxFilesToProcess)
{
state.Break();
}
...
As a workaround I can probably use .Take([xx])
on the TSource
before it is passed into the parallel loop but that might not be an option for a complex condition to break on.