I understand that calling task.Result
in an async
method can lead to deadlocks. I have a different twist on the question, though...
I find myself doing this pattern a lot. I have several tasks that return the same types of results, so I can await on them all at once. I want to process the results separately, though:
Task<int> t1 = m1Async();
Task<int> t2 = m2Async();
await Task.WhenAll(t1, t2);
Is it ok to call Result
here, since I know the tasks are now completed?
int result1 = t1.Result;
int result2 = t2.Result;
Or, should I use await
still...it just seems redundant and can be a bit uglier depending on how I need to process the results:
int result1 = await t1;
int result2 = await t2;
Update: Someone marked my question as a duplicate of this one: Awaiting multiple Tasks with different results. The question is different, which is why I didn't find it in my searches, though one of the detailed answers there does answer may question, also.