I have troubles understanding why this code:
// Example #1
foreach (var task in tasks)
{
task.Start();
task.Wait();
}
runs much, much faster than:
// Example #2
foreach (var task in tasks)
{
task.Start();
}
foreach (var task in tasks)
{
task.Wait();
}
While example #1 executes all tasks in 1-2 seconds, example #2 takes almost 20s to execute. Variable tasks
is of type Task[]
.
There is about a dozen of tasks in array, and each takes 500-1000ms to execute. There is no CPU bound, because tasks just send HTTP requests to server.
It doesn't make any sense for me.