await Task.Delay(delay)

某大神说de:

能想到用 Task.Delay(delay).ContinueWith(...) 实现的,算是不错。
能想到用 System.Threading.Timer 实现的,我就看他会不能正确使用 Timer 类(这个类实现了IDisposable接口)。
用 Thread.Sleep() 的,可以干掉了。

能写成这样的:
async Task SetTimeout(Action action, TimeSpan delay) {
await Task.Delay(delay);
action();
}
说明是天天玩async/await的,这个人对异步应该很熟悉。

posted on 2021-04-29 10:06  Shine-Zhong  阅读(1559)  评论(0编辑  收藏  举报

导航