1

The MSDN documentation for await and async gives the following simple example.

async Task<int> AccessTheWebAsync() { HttpClient client = new HttpClient(); Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); DoIndependentWork(); string urlContents = await getStringTask; return urlContents.Length; } 

It then states:

If AccessTheWebAsync doesn't have any work that it can do between calling GetStringAsync and awaiting its completion, you can simplify your code by calling and awaiting in the following single statement.

string urlContents = await client.GetStringAsync(); 

But if there is no work to be done between calling the method and its completion what are the benefits of making this method asynchronous?

3
  • 1
    Because the method execution itself is still asynchronous, so the code (UI) calling that piece of code is still responsive. It only means that since there is no block DoIndependentWork, there's no reason to buffer the Task first. Commented Dec 1, 2015 at 12:19
  • Thanks for the answers every one Commented Dec 1, 2015 at 12:35
  • you mean accessthewebasync is called by UI? That means the whole UI will have to wait then in await getStringTask? Commented Oct 9, 2017 at 1:26

3 Answers 3

4

The await keyword means that the Thread starting the GetStringAsync method may become free during the execution of that method.

So for example let's say this is a web-server (e.g. with 10 threads in a thread-pool, which are responsible for processing the requests). In that case between the beginning of the GetStringAsync call and it's finish that thread is free to do other stuff.

Meaning even with 10 thread you can serve more then 10 users on a very effective way. If there would be no await in your code (and GetStringAsync would not be async, but a blocking synchronous method) and 11 users would access it at the same time then one of them would have trouble, since there would be no free thread to process the 11. request.

If that call would be a blocking synchronous code then the execution would just stay there and do nothing while your network card would just wait for the HTTP response from the server. So basically with "await" you avoid threads which are just waiting and do nothing.

Sign up to request clarification or add additional context in comments.

1 Comment

Btw I wrote a blog post about this explaining all the stuffs which happen behind async/await: apmblog.dynatrace.com/2014/10/09/…
3

When you await an async method, the thread is freed to do other work. In the synchronous version, that thread would have been blocked.

1 Comment

Which is the whole point of this question. Free to do other work. Do what? There is no other work. In fact the whole thread awaits anyway.
1

In general Async-Await consumes lesser threads, improves scalability by reducing memory/thread usage. It also improves responsiveness as compared to synchronous code.

check Advantages of Async-Await

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.