0

I have the following code options:

foreach (var e in result.ExcludedEntity)
       await ProcessExcl.processEntity(e, e.ExclusionIdentification.UeiDuns);
await Task.WhenAll(result.ExcludedEntity.Select(async e =>
    await ProcessExcl.processEntity(e, e.ExclusionIdentification.UeiDuns)));

I realize the first one will run synchronously and the second will be asynchronous.

Is there a way to limit the second to by say running in batches of 20 (I don't even know if that is ideal)?

But I can tell you that running them all at once (sometimes 7000 results) runs slower than running it synchronously.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
KeithL
  • 5,348
  • 3
  • 19
  • 25
  • 4
    "I realize the first one will run sync and the 2nd will be async." No, they are both asynchronous. The first runs *sequentially* and the second runs *concurrently*. Whether operations are synchronous or asynchronous is independent of whether they're sequential or concurrent. – Servy Jul 01 '21 at 14:58
  • You can find some good solutions here: [How to limit the amount of concurrent async I/O operations?](https://stackoverflow.com/questions/10806951/how-to-limit-the-amount-of-concurrent-async-i-o-operations) You could also wait for the [`Parallel.ForEachAsync`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreachasync?view=net-6.0) API, that is going to be introduced with the upcoming .NET 6. – Theodor Zoulias Jul 01 '21 at 15:39

0 Answers0