I have below code, which should wait 10seconds. Problem is that it completes immediately, the WhenAll method is not working - what am I doing wrong here?
public class WhenAllIsNotWorking { public async Task myFunc() { await Task.Delay(10000); } public async void Init() { var tasks = new List<Task>(); for (var i = 0; i < 10; i++) { tasks.Add(new Task(async () => { await myFunc(); })); } foreach (var task in tasks) { task.Start(); } await Task.WhenAll(tasks); } } Edit, as I didn't mention this originally - above is oversimplified example of my real code - in reality I have a hierarchical tree of entities which I first traverse and register operations per entity (thus why I use new Task() with combination of task.Start()). Once I register all the operations, I then group them, and later do task.Start() on them which allow me to execute operations in ordered way per entity type. Of course that's what I would like to do, if it wasn't for the fact that WhenAll is not doing it's job here.
My solution, someone closed my question and I can't post answers anymore, anyhow, here is what I ended up doing - thanks for all your help!
public class WhenAllIsNotWorking { public async Task myFunc() { await Task.Delay(10000); } public async Task Init() { var tasks = new List<Func<Task>>(); for (var i = 0; i < 10; i++) { tasks.Add(async () => { await myFunc(); }); } var waitList = new List<Task>(); foreach (var task in tasks) { waitList.Add(Task.Run(task)); } await Task.WhenAll(waitList); } }
async voidasync Taskit still completes immediately.Init()? It should beawait Init();Taskconstructor is not async-friendly (this is not the best link, but I couldn't find a better one right now).