Home >Backend Development >C#.Net Tutorial >Asynchronous programming in C# using the Async and Await keywords

Asynchronous programming in C# using the Async and Await keywords

WBOY
WBOYforward
2023-09-02 19:45:02768browse

使用 Async 和 Await 关键字在 C# 中进行异步编程

#Asynchronous programming in C# is an effective way to solve blocked activities or delayed access. If an activity is blocked like this in the synchronization process, then the entire application will wait and take more time. The application stopped responding. Using asynchronous methods, the application can also continue to perform other tasks.

The async and await keywords in C# are used for asynchronous programming. Using them, you can use .NET Framework resources, .NET Core, and more. Asynchronous methods defined using the async keyword are called asynchronous methods.

For applications with GUI, please check the content queue if there are unprocessed tasks, take them out and process them first. The code is executed synchronously, and unprocessed tasks are completed first. If processing takes longer than expected, the application will display a Stopped Responding message.

Let us see what was discussed above -

private void OnRequestDownload(object sender, RoutedEventArgs e) {
   var req = HttpWebRequest.Create(_requestedUri);
   var res = req.GetResponse();
}

To solve the above problem, use async and await keywords -

private async void OnRequestDownload(object sender, RoutedEventArgs e) {
   var req= HttpWebRequest.Create(_requestedUri);
   var res = await req.GetResponseAsync();
}

The above is the detailed content of Asynchronous programming in C# using the Async and Await keywords. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Math functions in C#Next article:Math functions in C#