Is there a difference between calling .Result or await on known completed tasks? [duplicate]

旧街凉风 提交于 2020-08-06 13:13:11

问题


Is there any functional, performance, or risk of deadlock difference in the below code blocks?

Example 1:

await Task.WhenAll(task1, task2); 
var result1 = await task1; 
var result2 = await task2; 

Example 2:

await Task.WhenAll(task1, task2); 
var result1 = task1.Result;
var result2 = task2.Result; 

回答1:


Is there any functional, performance, or risk of deadlock difference in the below code blocks?

No, there isn't any such a case.

In both cases a task is created that will be completed when task1 and task2 would be completed.

Hence, when you write:

var result1 = await task1; 
var result2 = await task2;

the code will be executed synchronoulsy. You don't have to await for something, since you both task1 and task2 would have completed.

The same holds, for the second example, where you try to get their results.

var result1 = task1.Result;
var result2 = task2.Result; 

Since, the tasks have already completed, you don't block any thread calling thread or having any context switch etc.

Update

The only functional difference that exists between these two approaches is that the error handling is different. The await just unwraps an AggregateException, while .Result will just raise the exception.



来源:https://stackoverflow.com/questions/29809302/is-there-a-difference-between-calling-result-or-await-on-known-completed-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!