等待与任务。导致异步方法

做以下事情有什么区别:

async Task<T> method(){
var r = await dynamodb.GetItemAsync(...)
return r.Item;
}

async Task<T> method(){
var task = dynamodb.GetItemAsync(...)
return task.Result.Item;
}

就我而言,由于某种原因,只有第二种方法有效。第一种方法似乎永远不会结束。

159678 次浏览

task.Result is accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method. Once the result of an operation is available, it is stored and is returned immediately on subsequent calls to the Result property. Note that, if an exception occurred during the operation of the task, or if the task has been cancelled, the Result property does not return a value. Instead, attempting to access the property value throws an AggregateException exception. The only difference is that the await will not block. Instead, it will asynchronously wait for the Task to complete and then resume

await asynchronously unwraps the result of your task, whereas just using Result would block until the task had completed.

See this explanantion from Jon Skeet.