Reputation: 37
I want to use Async to perform a function over 3 different arrays.
Currently I have a nested foreach
loop over the 3 arrays, however that seems to break the actual intention of Async.
This is the method where I'm iterating over the code:
public void newclient(Form f, string[] username, string[] password, string[] server) {
foreach(string thisserver in server) {
foreach (string thispassword in password) {
foreach (string thisusername in username) {
Console.WriteLine(thisserver);
Task<string> Task = SpawnClient(f, thisusername, thispassword, thisserver);
Console.WriteLine(Task);
}
}
}
This is the Async method it's calling:
public async Task<string> SpawnClient(Form f, string nextusername, string nextpassword, string nextserver) {
///////////////dostuffhere
}
I have tried playing with things such as TaskCompletionSource
's, but I think I'm misunderstanding Async at the moment. It's my first time using it.
Upvotes: 1
Views: 764
Reputation: 35594
Perhaps you want this:
List<Task<string>> tasks = new List<Task<string>>();
foreach(string thisserver in server) {
foreach (string thispassword in password) {
foreach (string thisusername in username) {
Console.WriteLine(thisserver);
Task<string> task = SpawnClient(f, thisusername, thispassword, thisserver);
tasks.Add(task);
}
}
}
var allResults = await Task.WhenAll(tasks);
You'll need to redeclare newclient
as async Task
or (only if your newclient
is an event handler) async void
.
Upvotes: 2