I have some code where I’m loading up a list of tasks, then calling await WhenAll on that list. The tasks don’t get run. When I convert the list to an array and pass it in to Task.WaitAll, they do run. The code looks kind of like this:
private async Task SendMessages(List<Message> messages)
{
List<Task> tasks = new();
foreach (Message message in messages)
{
tasks.Add(SendMessageAsync(message ));
}
Task.WaitAll(tasks.ToArray()); // This works
// await Task.WhenAll(tasks); // This does not work - SendMessagesAsync never appears to run - the message never gets sent and breakpoints inside it don't get hit
}
public async Task SendMessagesAsync(Message message)
{
// Code to Send the message...
}
I can go ahead and just use Task.WaitAll of course but it’s unnerving to not understand the functionality. Is it possible the function is returning and all the SendMessagesAsync Tasks are being dissolved before they can run?