Await vs Task.Result in an Async Method

136,814

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

Share:
136,814

Related videos on Youtube

luis
Author by

luis

Updated on July 08, 2022

Comments

  • luis
    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.

  • luis
    luis over 8 years
    why is await not working in this case though, but Result does work
  • luis
    luis over 8 years
    why is await not working in this case though, but Result does work
  • Peter Duniho
    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
    vpalmu over 5 years
    Calling Result bare is a hidden deadlock.
  • vpalmu
    vpalmu over 5 years
    Calling Result bare is a hidden deadlock.
  • Onosa
    Onosa over 4 years
    Piggybacking off of Joshua, you can find out more about why it causes a hidden deadlock here: stackoverflow.com/questions/17248680/…
  • Luminous_Dev
    Luminous_Dev about 4 years
    its a bit old post, but did you do await Method() ?
  • user2953241
    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
    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
    user3012488 over 3 years
    I 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?