Await vs Task.Result in an Async Method
Solution 1
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.
Solution 2
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
Related videos on Youtube
luis
Updated on July 08, 2022Comments
-
luis almost 2 years
What's the difference between doing the following:
async Task<T> method(){ var r = await dynamodb.GetItemAsync(...) return r.Item; }
vs
async Task<T> method(){ var task = dynamodb.GetItemAsync(...) return task.Result.Item; }
In my case, for some reason, only the second works. The first one never seems to end.
-
Cyrus Downey over 6 yearsSo the only reason why I have found that a await could hang is from the below SO answers. I thought I would leave a comment here to help out future me. stackoverflow.com/questions/13489065/… stackoverflow.com/questions/9895048/…
-
Jesper Mygind over 5 yearsTry await dynamodb.GetItemAsync(...).ConfigureAwait(false)
-
-
luis over 8 yearswhy is await not working in this case though, but Result does work
-
luis over 8 yearswhy is await not working in this case though, but Result does work
-
Peter Duniho over 8 years@luis: Lacking any other information, the only answer I see to that is that it's not actually working in the
await
case. You just mistakenly think it does because the method itself returns. But the task being awaited likely does not complete either way. If you want an answer to that (which is a different question than the one you asked), you need to post a new question, stating that clearly, and providing a good, minimal, complete code example that reliably reproduces the problem. -
vpalmu over 5 yearsCalling
Result
bare is a hidden deadlock. -
vpalmu over 5 yearsCalling
Result
bare is a hidden deadlock. -
Onosa over 4 yearsPiggybacking off of Joshua, you can find out more about why it causes a hidden deadlock here: stackoverflow.com/questions/17248680/…
-
Luminous_Dev about 4 yearsits a bit old post, but did you do
await Method()
? -
user2953241 almost 4 years@Joshua what do you mean with hidden deadlock? Deadlock as in multiple threads waiting on each other or do you mean a thread-blocking operation which counteracts async?
-
vpalmu almost 4 years@user2953241:
.Result
or all of its possible variants cause a threadpool thread to wait for a job on the thread pool to finish. This can exhaust all threads on the threadpool leaving none to perform the actual work. -
user3012488 over 3 yearsI have the same issue as @luis, I'm retrieving data from SQL using dapper. When using async, after some executions, I'm getting a Connection is closed error. While, after replacing the await with
.Result
, works like a charm. Any explanation?