C# 多线程 04-使用任务平行库 02-使用任务执行基本的操作
🏷️ 《C# 多线程》
使用任务执行基本的操作
csharp
/// <summary>
/// 使用任务执行基本的操作
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
// 直接调用,同步执行,不是线程池中的线程
TaskMethod("Main Thread Task");
Task<int> task = CreateTask("Task 1");
// 启动任务
// 该任务会被放置在线程池中
task.Start();
// 等待结果(直到任务返回前,主线程一直处于阻塞状态)
int result = task.Result;
Console.WriteLine($"Result is: {result}");
task = CreateTask("Task 2");
// 同步运行该任务
// 该任务会运行在主线程中,运行结果同直接调用一样
task.RunSynchronously();
result = task.Result;
Console.WriteLine($"Result is: {result}");
task = CreateTask("Task 3");
Console.WriteLine(task.Status); // Created
// 启动任务
task.Start();
// 这里没有阻塞主线程,而是在任务完成前循环打印任务状态
// 任务状态分别为:Created、Running 和 RanToCompletion
while (!task.IsCompleted)
{
Console.WriteLine(task.Status); // Running 和 RanToCompletion
Thread.Sleep(TimeSpan.FromSeconds(0.5));
}
Console.WriteLine(task.Status); // RanToCompletion
result = task.Result;
Console.WriteLine($"Result is: {result}");
Console.ReadLine();
}
static Task<int> CreateTask(string name)
{
return new Task<int>(() => TaskMethod(name));
}
static int TaskMethod(string name)
{
Console.WriteLine($"Task {name} is running on a thread id {Thread.CurrentThread.ManagedThreadId}. Is thread pool thread: {Thread.CurrentThread.IsThreadPoolThread}");
Thread.Sleep(TimeSpan.FromSeconds(2));
return 42;
}
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
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
打印结果
txt
Task Main Thread Task is running on a thread id 1. Is thread pool thread: False
Task Task 1 is running on a thread id 3. Is thread pool thread: True
Result is: 42
Task Task 2 is running on a thread id 1. Is thread pool thread: False
Result is: 42
Created
Task Task 3 is running on a thread id 4. Is thread pool thread: True
Running
Running
Running
Running
Running
RanToCompletion
Result is: 42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14