为什么这个程序挂起了
本文关键字:挂起 程序 为什么 | 更新日期: 2023-09-27 18:29:08
我有以下程序:
static void Main(string[] args) { RunTest(); }
private static void RunTest() {
DoIOWorkFiveTimesAsync().Wait();
}
private static async Task DoIOWorkFiveTimesAsync() {
for (int i = 0; i < 5; ++i) {
Console.WriteLine("Before: " + i);
await DoIOWorkAsync();
Console.WriteLine("After: " + i);
}
}
private static Task DoIOWorkAsync() {
Console.WriteLine("Doing work...");
return new Task(() => Thread.Sleep(1500));
}
我希望看到:
Before: 1
Doing work...
After: 1
Before: 2
Doing work...
After: 2
Before: 3
Doing work...
After: 3
Before: 4
Doing work...
After: 4
Before: 5
Doing work...
After: 5
但相反,它变成了:
Before: 1
Doing work...
再也没有进展。我试着理解C#5中的async/await特性,但总是没有效果。再次,我无法解释。
问题是您使用的是return new Task(() => Thread.Sleep(1500));
而不是Task.Run
。
new Task
实际上并没有启动任务,这将导致await
永远不会触发。
相反,请尝试:
private static Task DoIOWorkAsync() {
Console.WriteLine("Doing work...");
return Task.Run(() => Thread.Sleep(1500));
}
或者,更好的是:
private static async Task DoIOWorkAsync() {
Console.WriteLine("Doing work...");
await Task.Delay(1500);
}
很简单,您返回了一个Task
,但您没有启动它。
如果您更改代码如下:
private static Task DoIOWorkAsync()
{
Console.WriteLine("Doing work...");
Task work = new Task(() => Thread.Sleep(1500));
work.Start();
return work;
}
正如你所期望的那样。