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?
Most of your questions are answered in the official documentation and also in an intro post that I wrote.
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) supportasync
directly; others need you to write simpleasync
-compatible wrappers.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.
The benefits of asynchronous operations are:
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.
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:
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.
After.
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.
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.
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.