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
Release: 2023-09-02 19:45:02
forward
767 people have browsed it

使用 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();
}
Copy after login

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();
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template