I have a situation where I want to fire off a user-defined number of tasks asynchronously and wait for all of them to complete. Simplified, here's what I'm dealing with:
[TestMethod]
public async Task Start() {
var numDrivers = 2;
while (numDrivers != 0) {
var rnd = new Random();
var r = rnd.Next(itemArray.Count);
var target = itemArray[r];
var proxyDriver = GetProxiedDriver();
Task.Run(() => HandleIntro(proxyDriver, target));
numDrivers--;
}
}
For some context - these are Selenium webdrivers getting spun up to run some UI tests. I see the browsers pop up, but as soon as the last Task.Run()
completes, all execution stops. How do I fire off n
drivers to run asynchronously while waiting for them all to complete before stopping execution?
I've tried await Task.Run(() => HandleIntro(proxyDriver, target));
but this awaits the task and they don't run simultaneously.
HandleIntro:
private async Task HandleIntro(FirefoxDriver driver, string target) {
// do stuff
}