How does await async work in C# [closed]

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.


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.