何时使用Task.Delay,什么时候用的Thread.Sleep? [英] When to use Task.Delay, when to use Thread.Sleep?
本文介绍了何时使用Task.Delay,什么时候用的Thread.Sleep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于何时使用任务有好的规则(S)。延迟与<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep.aspx\">Thread.Sleep?
- 具体来说,有没有提供一个有效/高效比其他? 的最小值
- 最后,由于Task.Delay导致异步/的await状态机的上下文切换,有没有使用它的开销?
推荐答案
使用 Thread.sleep代码
当你想阻止当前线程。
Use Thread.Sleep
when you want to block the current thread.
使用 Task.Delay
当你想不阻塞当前线程的逻辑延迟。
Use Task.Delay
when you want a logical delay without blocking the current thread.
效率不应该用这些方法的首要关注的问题。他们的主要现实世界的用途是作为重试定时器用于I / O操作,这是秒级的,而不是毫秒。
Efficiency should not be a paramount concern with these methods. Their primary real-world use is as retry timers for I/O operations, which are on the order of seconds rather than milliseconds.
这篇关于何时使用Task.Delay,什么时候用的Thread.Sleep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文