rory.ap
rory.ap

Reputation: 35270

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() (i.e. 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"?

Upvotes: 3

Views: 782

Answers (1)

usr
usr

Reputation: 171178

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.

Upvotes: 3

Related Questions