I have two tasks that can be run in parallel to increase performance:
var task1 = _service.DoStuffAsync();
var task2 = _service.DoOtherStuffAsync();
await Task.WhenAll(task1, task2);
Now, I'm sure that these tasks are done. But per my understanding (and some real life headaches), if I call .Result
on these tasks, I could cause a deadlock, even though they are complete?
From what I was reading,await
ing a completed task simply returns the result, so it seems like that is the way to go here. That makes my code look funky:
var task1 = _service.DoStuffAsync();
var task2 = _service.DoOtherStuffAsync();
await Task.WhenAll(task1, task2);
var result1 = await task1;
var result2 = await task2;
Is this the correct way to solve this problem and get the results of both my tasks? If not, what's the problem? Is there a better way to unwrap the tasks without calling .Result
on them?