I am firing commands to IP cameras, about thousands of them, The code and network requests are in ConnectCam method. I use the below pattern to fire them all at once using Task.WhenAny(). Does this have any potential bottlenecks? Should I throttle the requests?
List<Task> tasks = deviceList.Select(x => ConnectCamAsync(ct, x.IpAddress, x.UserName, x.PassWord)).ToList();
var orgCount = tasks.Count();
while (tasks.Count() > 0)
{
ct.ThrowIfCancellationRequested();
try
{
var firstFinishedTask = await Task.WhenAny(tasks);
tasks.Remove(firstFinishedTask);
await firstFinishedTask;
}
finally
{
UpdateProgress(0, orgCount, orgCount - tasks.Count());
}
}