简体   繁体   English

System.Threading.Thread.Sleep(1000)vs. System.Threading.Tasks.Task.Delay(1000).Wait()

[英]System.Threading.Thread.Sleep(1000) vs. System.Threading.Tasks.Task.Delay(1000).Wait()

In learning .NET 4.5, I get the impression that System.Threading.Tasks.Task.Delay(1000).Wait() (ie the blocking delay) is preferred over System.Threading.Thread.Sleep(1000) . 在学习.NET 4.5时,我得到的结论是System.Threading.Tasks.Task.Delay(1000).Wait() (即阻塞延迟)优于System.Threading.Thread.Sleep(1000) Is that true, and, if so, why? 这是真的,如果是这样,为什么? Is it simply because the .NET marching orders have always been "use the latest technology whenever possible"? 仅仅因为.NET行进命令一直是“尽可能使用最新技术”吗?

Thread.Sleep(1000) is preferred because it is the idiomatic way to perform a synchronous wait. Thread.Sleep(1000)是首选,因为它是执行同步等待的惯用方法。 It has been the standard for 15 years now. 它已成为15年的标准。 There is nothing wrong with it. 没有什么问题。

Task.Delay(1000).Wait() does the same thing. Task.Delay(1000).Wait()做同样的事情。 It is harder to understand, slower to type, slower to execute and, in my mind, backwards thinking. 理解起来比较难,打字速度慢,执行速度慢,而且在我看来,反向思考。

If you want a synchronous operation call the synchronous API made for that. 如果要进行同步操作,请调用为此创建的同步API。 Sleeping is not special in this regard. 睡觉在这方面并不特别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Threading.Timer vs System.Threading.Thread.Sleep resolution - .NET Timer不使用系统时钟分辨率 - System.Threading.Timer vs System.Threading.Thread.Sleep resolution - .NET Timer not using system clock resolution 使用async和await与System.Threading.Thread.Sleep - Using async and await with System.Threading.Thread.Sleep 什么线程睡眠方法最精确:Monitor.Wait vs System.Timer vs DispatchTimer vs Threading.Timer - What Thread sleep method is most precise: Monitor.Wait vs System.Timer vs DispatchTimer vs Threading.Timer 等待句柄和System.Threading.Tasks命名空间 - Wait Handles and the System.Threading.Tasks namespace 找不到 System.Threading.Tasks.Task 方法 - System.Threading.Tasks.Task Method Not Found 每个新的System.Threading.Tasks.Task()都会创建一个新线程吗? - Do each new System.Threading.Tasks.Task() creates a new thread? System.Threading.Tasks - > Continuations - System.Threading.Tasks -> Continuations 有没有办法发信号通知System.Threading.Tasks.Task? - Is there a way to signal a System.Threading.Tasks.Task complete? 跟踪已完成/正在运行的System.Threading.Tasks.Task对象 - Tracking completed/running System.Threading.Tasks.Task objects 无法将类型'void'隐式转换为'System.Threading.Tasks.Task' - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM