c# async await

cyang Lv6

Asynchronous programming
Task asynchronous programming model

An async method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete. In the meantime, control returns to the caller of the method, as the example in the next section shows.

key points

  • C# supports simplified approach, async programming, that leverages asynchronous support in the .NET runtime. The compiler does the difficult work that the developer used to do, and your application retains a logical structure that resembles synchronous code. As a result, you get all the advantages of asynchronous programming with a fraction of the effort.

  • await: The method usually includes at least one await expression, which marks a point where the method can’t continue until the awaited asynchronous operation is complete. In the meantime, the method is suspended, and control returns to the method’s caller. The marked async method can use await to designate suspension points. The await operator tells the compiler that the async method can’t continue past that point until the awaited asynchronous process is complete. In the meantime, control returns to the caller of the async method.
    When the await operator is applied to the operand that represents an already completed operation, it returns the result of the operation immediately without suspension of the enclosing method.

Thread

  • Async methods are intended to be non-blocking operations. An await expression in an async method doesn’t block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

  • The async and await keywords don’t cause additional threads to be created. Async methods don’t require multithreading because an async method doesn’t run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Threading.Tasks;

namespace MutilTaskConsoleApp
{
public class Program
{
public static void Main(string[] args)
{
// Create a new task, but don't run it yet.
Task task1 = new Task(() => Console.WriteLine("Hello World!"));

Task2();
Task<int> task3 = Task3();
Task<int> task4 = Task4(10);
Task<int> task5 = Task5(10);

int res3 = task3.Result;
int res4 = task4.Result;
int res5 = task5.Result;

Console.WriteLine($"{res3}, {res4}, {res5}");

// Start the task1
task1.Start();
// Wait for the task1 to complete
task1.Wait();
}

public static async void Task2()
{
await Task.Run(() =>
{
Console.WriteLine(nameof(Task2) + " start.");
Task.Delay(1000);
Console.WriteLine(nameof(Task2) + " finished.");
});
}

public static async Task<int> Task3()
{
int res = 0;

await Task.Run(() =>
{
Console.WriteLine(nameof(Task3) + " start.");
Task.Delay(500);
res++;
Console.WriteLine(nameof(Task3) + " finished.");
});

return res;
}

public static async Task<int> Task4(int i)
{
int res = 0;

await Task.Run(() =>
{
Console.WriteLine(nameof(Task4) + " start.");
Task.Delay(200);
res += i;
Console.WriteLine(nameof(Task4) + " finished.");
});

return res;
}

public static async Task<int> Task5(int i)
{
int res = 0;

Console.WriteLine(nameof(Task5) + " start.");
await Task.Delay(100);
res -= i;
Console.WriteLine(nameof(Task5) + " finished.");

return res;
}
}
}
  • 标题: c# async await
  • 作者: cyang
  • 创建于 : 2022-12-05 12:55:23
  • 更新于 : 2022-12-05 12:55:23
  • 链接: https://blog.cyang.tech/2022/12/05/csharp async await/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
此页目录
c# async await