A set of technologies in the .NET Framework for building web applications and XML web services.
Hello @Dondon510 ,
Thanks for reaching out
To clarify your question:
- The List<Task> is not automatically cleared after await Task.WhenAll(tasks).
- Task.WhenAll(tasks) simply waits for all the tasks in the list to complete. It does not modify the list itself. The list will still contain references to the completed tasks after the await.
- If you're inside a loop and reusing the same List<Task> instance, you should call tasks.Clear() after Task.WhenAll() to avoid accumulating completed tasks. This helps prevent unnecessary memory usage and ensures you're only waiting on fresh tasks in each iteration.
Here’s the corrected version:
int i = 0;
List<Task> tasks = new List<Task>();
do
{
tasks.Add(Do_Test1(i));
// Add other tasks as needed
// tasks.Add(Do_Test2(i));
// tasks.Add(Do_Test3(i));
await Task.WhenAll(tasks);
tasks.Clear(); // Clear the list for the next iteration
i++; // Optional: increment i if needed
}
while (someCondition); // Add your loop condition
Final thoughts:
Task.WhenAll() does not clear the list.
You should clear the list manually if you're reusing it.
Hope my answer would help.