The advantage is that the control is returned to the caller of the current method. Your async method will "resume" when the task awaited is completed.
This is very useful because it removes the need to manage threads and/or callbacks.
Edit:
Here's why you would want (or not) to use async/await. Let's say you have a network call like this:
public RequestResult Request(string queryString)
{
var response = connection.Request(queryString);
}
This is a blocking call, it'll stop the current executing thread and wait until the call is completed. There could be an Async
version of the Request(string)
method, usually suffixed with Async
. If so, you can await
it in an async Task
method.
public RequestResult Request(string queryString)
{
var response = connection.Request(queryString);
}
The control will be released until the task is completed, allowing the caller to continue. This is very useful for a GUI application. You can bind an async
method to an event, have it fired, start the request and keep the UI updating while the request completes.
Before async/await
, the prefered method was using Begin/End
methods. You would pass a delegate to the Begin method and it would get called when the task was done. I want to point out, this can still be used! Here are more details on async patterns. async/await
is called TAP, Task based async programming.
Edit: The C# compiler will take the rest of the method after an await
keyword and make it sort of a callback (internally).