Asynchronous programming with the async and await keywords
The async
and await
keywords were announced in C# 5.0 and became the latest and greatest things in C# asynchronous programming. Developed from the TAP pattern, C# integrates these two keywords into the language itself so that it makes it simple and easy to read. Using these two keywords, the Task
and Task<TResult>
classes still become the core building blocks of asynchronous programming. We will still build a new Task
or Task<TResult>
data type using the Task.Run()
method, as discussed in the previous section.
Now let's take a look at the following code, which demonstrates the async
and await
keywords, which we can find in the AsyncAwait.csproj
project:
public partial class Program { static bool IsFinish; public static void AsyncAwaitReadFile() { IsFinish = false; ReadFileAsync(); //do other work while file is read int i = 0; do {...