Asynchronous Programming in C#
Asynchronous programming allows a program to perform long-running tasks without blocking the main thread. C# provides the async and await keywords to simplify asynchronous programming. Here's an example code that demonstrates how to use async and await to download a web page:
static async Task<string> DownloadWebPageAsync(string url)
{
using (HttpClient client = new HttpClient())
{
return await client.GetStringAsync(url);
}
}
static async Task Main(string[] args)
{
string result = await DownloadWebPageAsync("https://www.example.com");
Console.WriteLine(result);
}
No comments:
Post a Comment