Questions tagged «async-await»

这涵盖了使用async和await关键字的各种编程语言支持的异步编程模型。

3
如何声明一个未启动的任务,将等待另一个任务?
我已经完成了此单元测试,但我不明白为什么“ await Task.Delay()”不等待! [TestMethod] public async Task SimpleTest() { bool isOK = false; Task myTask = new Task(async () => { Console.WriteLine("Task.BeforeDelay"); await Task.Delay(1000); Console.WriteLine("Task.AfterDelay"); isOK = true; Console.WriteLine("Task.Ended"); }); Console.WriteLine("Main.BeforeStart"); myTask.Start(); Console.WriteLine("Main.AfterStart"); await myTask; Console.WriteLine("Main.AfterAwait"); Assert.IsTrue(isOK, "OK"); } 这是单元测试的输出: 这怎么可能是“等待”不等待,而主线程继续呢?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.