Async Await in C#
Async Await in C#
Let us say we have an activity which is blocked (in a synchronous process), then the complete application will wait, and it eventually will take more time to load or process. The application might stop responding to the request sent. Using the asynchronous approach, the application will continue to respond even if the other tasks are still pending.
The async and await keywords are used in context of asynchronous programming. The methods which are defined using the async keyword are known as async methods.
Example –
Let us say we have an application which checks the content of the queue and if an unprocessed task is there, it takes it out and processes it first.
So, the behavior here is that the code executes synchronously, and the unprocessed task is completed first. The outcome of this would be that the application will stop responding to messages if the processing takes more time than the expected time.
private void OnGetRequest(object sender, RoutedEventArgs e) {
var req = HttpWebRequest.Create(requestedUri);
var res = req.GetResponse();
}
To solve this issue, we will use async and await −
private async void OnGetRequest(object sender, RoutedEventArgs e) {
var req= HttpWebRequest.Create(requestedUri);
var res = await req.GetResponseAsync();
}
