Async Sample: Asynchronous programming with async and await in C#
This sample is a WPF application written in C# from the Asynchronous programming with async and await in C# tutorial. The article gives an overview of asynchronous programming, including when to use it and how to write an async method. This sample contains an async function that is used as an illustration.
async and await provide all the advantages of traditional asynchronous programming, but with much less effort from the developer. The compiler does the difficult work that the developer used to do, yet the code retains a logical structure that resembles synchronous code.
The example async function in this sample (named ProcessUrlAsync
) uses an HttpClient method to download the contents of a website.
The code for the MainWindow.xaml.cs file from this sample is included in the article.
Sample prerequisites
This sample is written in C# and targets .NET 6 running on Windows. It requires the .NET 6.0 SDK.
Building the sample
To download and run the sample, follow these steps:
- Download and unzip the sample.
- In Visual Studio (2019 or later):
- On the menu bar, choose File > Open > Project/Solution.
- Navigate to the folder that holds the unzipped sample code, and open the C# project (.csproj) file.
- Choose the F5 key to run with debugging, or Ctrl+F5 keys to run the project without debugging.
- From the command line:
- Navigate to the folder that holds the unzipped sample code.
- At the command line, type
dotnet run
.