I have a list of events and I'd like to fetch the performances for each event. Now I want to have a max of 10 'agents' that run in parallel and not more.
This is what the code would look like, except I want to make sure that these are not run all in parallel. There might be 1000s of elements in allEvents. I'd like to limit it to 10 in parallel at max.
public async Task RefreshAll(Event[] allEvents)
{
var allPerformances = (await Task.WhenAll(allEvents.Select(CollectPerformancesForEvent))).SelectMany(x => x);
// persist allPerformances
}
private async Task<Performance[]> CollectPerformancesForEvent(Event @event)
{
// some API call collecting all performances for @event...
await Task.Delay(500);
return new[]{ new Performance() };
}