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 (cancellationToken.IsCancellationRequested) { state.Stop(); return; } // some process on the item Process(item); }); However, since I have a process that needs to be executed asynchronously, I switched to Parallel.ForEachAsync. The ForEachAsync does not have the Stop() method, I'm able to break the loop as the following, but I'm wondering if this is the most effective way of breaking the loop (in other words, the loop needs to stop ASAP when it receives the cancellation request).
await Parallel.ForEachAsync(items, async (item, state) => { if (cancellationToken.IsCancellationRequested) { return; } // some async process on the item await ProcessAsync(item); });