Async Await in C#
Jan 08, 2021 08:28 0 Comments C# (C-Sharp) PARTH

                        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();

}

 

 

 

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect