简体   繁体   中英

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) . Is that true, and, if so, why? Is it simply because the .NET marching orders have always been "use the latest technology whenever possible"?

Thread.Sleep(1000) is preferred because it is the idiomatic way to perform a synchronous wait. It has been the standard for 15 years now. There is nothing wrong with it.

Task.Delay(1000).Wait() does the same thing. 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. Sleeping is not special in this regard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM