How does await async work in C#

30,430

Solution 1

MSDN explains everything.

I understand though that sometimes vanilla docs (particularly from MSDN) can be difficult to apply to your particular situation, so let's go over your points.

Question # 1: Is this assumption correct or the code below the await keyword is still executed?

The code below the "await" keyword will only be executed when the async call completes. In the meantime, since your method is marked "async", control will be returned to the caller of your method until your method completes. From the MSDN link above:

Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();

// The await operator suspends AccessTheWebAsync. 
//  - AccessTheWebAsync can't continue until getStringTask is complete. 
//  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
//  - Control resumes here when getStringTask is complete.  
//  - The await operator then retrieves the string result from getStringTask. 
string urlContents = await getStringTask;

I think the comments are quite explanatory.

Secondly suppose I called a service method async and need to return its result. The return statement is below the await keyword.

Question # 2: When is the return statement hit, after the async call has completed or before it?

After.

Question # 3: I want to use the result of that service call and async operation wont help cause I want the calling method to be hit when the result has been returned. I understand this can be done using the Result property which makes the call synchronus. But then what is the use of async in DB operations cause they are the ones that actually take 80% of the time in most apps.

Suppose you need to make three unrelated DB queries to complete your service, then perform a calculation based on the results, then finish. If you did this sequentially, you'd have to wait until each operation completes. If you use async calls, then C# will run the three queries in parallel and your service might finish much sooner.

Also, operations that return Task can be used as Futures. See MSDN on Futures where several patterns are discussed on how to parallelize work based on futures and merge the results.

If your service only needs one DB call then it will definitely be worse for you to call it async.

Question # 4: How can I use async with DB operations? Is it possible and recomended?

ADO.NET now includes async methods ReadAsync and NextResultAsync .

It is definitely possible, as for recommended this discussion is much more complete than I could write here.

Question # 5: In which scenraio will async operations be useful it seems that every api is just making async operations now without a reason? or did I miss the point of using async ops?

async operations are very useful to easily parallelize any long running operations without running into threading problems. If your method only does one thing, or a sequence of simple (fast) things, then yes it is useless to go async. However if you have more than one long running operations, it is far easier and less error prone to parallelize them via async than it is to do it managing threads.

Solution 2

Most of your questions are answered in the official documentation and also in an intro post that I wrote.

Question # 4: How can I use async with DB operations? Is it possible

Entity Framework 6 (currently in Beta) supports async. Lower-level database APIs support asynchronous operations in one way or another. Some of them (e.g., SQLite) support async directly; others need you to write simple async-compatible wrappers.

... and recomended?

Yes, unless you are writing a front-end server (e.g., ASP.NET) that talks to a non-scalable single database machine on the back end. In that specific case, there's no point in making your front end scale because your back end can't scale to match it anyway.

Question # 5: In which scenraio will async operations be useful it seems that every api is just making async operations now without a reason? or did I miss the point of using async ops?

The benefits of asynchronous operations are:

  1. On the client (UI) side, your application remains responsive.
  2. On the server side, your application scales better.
Share:
30,430
Afraz Ali
Author by

Afraz Ali

Updated on May 26, 2020

Comments

  • Afraz Ali
    Afraz Ali almost 4 years

    I am trying to understand how await async work in C# and one thing is confusing me a lot. I understand that any method that uses await keyword must be marked with async. My understanding is that when a line with await keyword is hit the code below that line is not executed. An async operation is started to carry on the statement in the await line and the control is returned to the calling method which can proceed with the execution.

    Question # 1: Is this assumption correct or the code below the await keyword is still executed?

    Secondly suppose I called a service method async and need to return its result. The return statement is below the await keyword.

    Question # 2: When is the return statement hit, after the async call has completed or before it?

    Question # 3: I want to use the result of that service call and async operation wont help cause I want the calling method to be hit when the result has been returned. I understand this can be done using the Result property which makes the call synchronus. But then what is the use of async in DB operations cause they are the ones that actually take 80% of the time in most apps.

    Question # 4: How can I use async with DB operations? Is it possible and recomended?

    Question # 5: In which scenraio will async operations be useful it seems that every api is just making async operations now without a reason? or did I miss the point of using async ops?

    What I mean by saying that api are making asyn methods without a reason is because methods have to return something and untill that calculation is made how can they return so in essense won't the call be still blocking in sense that it will be useless until result is returned?

  • Afraz Ali
    Afraz Ali almost 11 years
    Wow! thanks mate, you explained everything in a very easy to understand manner. I went through MSDN but I saw a diagram with a lot of numbers explaining which line will execute when, and it made me even more confused. Thanks for your reply!
  • joelc
    joelc almost 8 years
    Simple but perfectly stated benefits at the end of your answer.
  • Manachi
    Manachi about 6 years
    Your quoted code references "AccessTheWebAsync" which is not defined/included in the quoted code. Assuming it's the container method.