My problem is that when a task has a call to Task.WhenAll () (launching other tasks), the WhenAll () line causes the consumption code to continue executing, unlike what I expect. Thus, the following code exits "complete" immediately after pressing Task.WhenAll (), and not after completing all tasks in its argument.
// Just a simple async method public Task DoWorkAsync() { return Task.Factory.StartNew( () => { // Working }); } // This one used the previous one with Task.WhenAll() public Task DoLoadsOfWorkAsync() { return Task.Factory.StartNew( async () => { // Working // This line makes the task return immediately await Task.WhenAll(DoWorkAsync(), DoWorkAsync()); // Working }); } // Consuming code await DoLoadsOfWorkAsync(); Console.WriteLine("finished");
I would expect WriteLine () to be called when the last line of DoLoadsOfWorkAsync () is executed.
What am I doing wrong? Thanks in advance.
source share