-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathAsyncAwaitTest.cs
264 lines (235 loc) · 6.87 KB
/
AsyncAwaitTest.cs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace TestCases
{
class AsyncAwaitTest
{
public async static Task TestRun()
{
Console.WriteLine("Start: TestRunSub1..");
await TestRunSub();
Console.WriteLine("Result: TestRunSub1 complete..");
}
async static Task TestRunSub()
{
int cnt = 0;
var res = await Task.Run<int>(() =>
{
for (int i = 0; i < 100000; i++)
cnt++;
return cnt;
});
Console.WriteLine($"Result:{res}");
}
public async static Task TestRun1()
{
Console.WriteLine("Start: TestRunSub1..");
await TestRunSub1();
Console.WriteLine("Result: TestRunSub1 complete..");
}
async static Task TestRunSub1()
{
AsyncAwaitTest1 test1 = null;
test1.a = "xxxxxx";
await Task.Run<int>(() =>
{
Console.WriteLine($"run 000");
return 0;
});
Console.WriteLine($"TestRunSub1 complete");
}
public async static Task TestRun4()
{
Console.WriteLine("Load 1");
await TestLoad("Load 1 complete");
Console.WriteLine("Load 2");
await TestLoad("Load 2 complete");
Console.WriteLine("Load 3");
await TestLoad("Load 3 complete");
}
static async Task TestLoad(string msg)
{
await Task.Delay(1000);
Console.WriteLine(msg);
}
public static async Task<int> TestRun2()
{
try
{
return await Task.Run(async () =>
{
Console.WriteLine("Start");
await Task.Delay(0);
Console.WriteLine("End");
return 0;
});
}
catch (System.Exception e)
{
Console.WriteLine($"{e.Message}\n{e.Data["StackTrace"]}\n{e.ToString()}");
throw e;
}
}
public static async Task<int> TestRun3()
{
try
{
return await Task.Run(async () =>
{
Console.WriteLine("Start");
await Task.Delay(0);
Console.WriteLine("End");
return 0;
});
}
catch (System.Exception e)
{
Console.WriteLine($"{e.Message}\n{e.Data["StackTrace"]}\n{e.ToString()}");
throw e;
}
}
/*public static async void TestRun4()
{
List<Task> tasks = new List<Task>();
for (int i = 1; i <= 3; ++i)
{
string path = $"{i}.txt";
Console.WriteLine(path);
if (!System.IO.File.Exists(path))
System.IO.File.CreateText(path);
Task task = Task.Run(async () =>
{
return await readText(path);
});
tasks.Add(task);
}
await Task.WhenAll(tasks);
}
private static async Task<List<string>> readText(string path)
{
Console.WriteLine(path);
List<string> lines = new List<string>();
using (System.IO.StreamReader reader = System.IO.File.OpenText(path))
{
string line;
while (null != (line = await reader.ReadLineAsync()))
{
lines.Add(line);
}
}
return lines;
}*/
public async static void TestRun5()
{
Console.WriteLine("TestConditionAwait Start ");
await DoConditionAwait();
Console.WriteLine("TestConditionAwait End");
}
static int v = 0;
static async Task DoConditionAwait()
{
if (v > 0)
{
await Task.Delay(1000);
}
Console.WriteLine("DoConditionAwait End");
}
class AsyncAwaitTest1
{
public string a;
}
public static class TestClass
{
public static async Task Show1()
{
await Task.CompletedTask;
}
public static void Show2()
{
Show3();
}
public static async void Show3()
{
Console.WriteLine("Show3");
await Task.CompletedTask;
}
}
public static void TestRun6()
{
InitAsync();
}
private static async void InitAsync()
{
Console.WriteLine("a1");
TestClass.Show2();
}
public async static void TestRun7()
{
int i;// = await TestRun7Sub(0);
//Console.WriteLine(i); // 输出0
i = await TestRun7Sub(1);
Console.WriteLine(i);
}
public static async Task<int> TestRun7Sub(int i)
{
if (i > 0)
{
// 注意:这个分支没有await
return 1;
}
else
{
await Task.Delay(1);
return 0;
}
}
public static void TestRun8()
{
Console.WriteLine("a");
Task.Run(() => Foo1());
Console.WriteLine("b");
Task.Run(() => Foo2());
Console.WriteLine("c");
}
private static async Task Foo1()
{
Console.WriteLine("1");
await Task.Delay(1000);
Console.WriteLine("2");
}
private static async Task Foo2()
{
Console.WriteLine("3");
await Task.Delay(1000);
Console.WriteLine("4");
}
public static void TestRun9()
{
var list = new List<int>();
for (var i = 0; i < 3; i++)
{
list.Add(i + 1);
}
TestRun9Sub(list.AsReadOnly());
}
static async void TestRun9Sub(System.Collections.ObjectModel.ReadOnlyCollection<int> roc)
{
int cnt = 0;
// 1. must be ReadOnlyCollection<T>
foreach (var val in roc)
{
cnt++;
if (cnt > 3)
throw new Exception();
Console.WriteLine(val);
// 2. must await a async task
await Task.Delay(1);
}
// it will stuck at some point, and becomes a infinite loop
}
}
}