-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCommonProgram.cs
More file actions
76 lines (66 loc) · 1.99 KB
/
Copy pathCommonProgram.cs
File metadata and controls
76 lines (66 loc) · 1.99 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Otus.AsyncAwait
{
public class CommonProgram
{
public async Task Execute1Async()
{
await WaitAndWriteStartAsync();
Console.WriteLine("finish");
}
public async Task Execute2Async()
{
var task = StartAsync();
Console.WriteLine("finish");
await task;
}
public async Task Execute3Async()
{
Console.WriteLine($"pre-start. ThreadId {Environment.CurrentManagedThreadId}");
await StartAsync();
Console.WriteLine($"after-start. ThreadId {Environment.CurrentManagedThreadId}");
}
public async Task Execute4Async()
{
//Console.WriteLine($"pre-start. ThreadId {Environment.CurrentManagedThreadId}");
var task = DelayAsync();
while (!task.IsCompleted)
{
Console.WriteLine($"task state >> {task.Status}");
//await Task.Delay(TimeSpan.FromSeconds(1));
}
Console.WriteLine($"task state after while: {task.Status}");
await task;
Console.WriteLine($"task state after await: {task.Status}");
}
private async Task WaitAndWriteStartAsync()
{
await Task.Delay(2000);
Console.WriteLine("Start");
}
private async Task StartAsync()
{
await Task.Delay(2000);
//await Task.Delay(0);
Console.WriteLine("start");
}
private async Task DelayAsync()
{
await Task.Delay(2000);
}
private async Task Execute2Async_2()
{
var task = StartAsync();
while (!task.IsCompleted)
{
await Task.Delay(1000);
}
Console.WriteLine("finish");
await task;
}
}
}