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

When learning about .NET 4.5, it seems like System.Threading.Tasks.Task.Delay(1000).Wait()

(i.e. blocking delay) is preferred System.Threading.Thread.Sleep(1000)

. Is this true, and if so, why? Is it simply because .NET step-by-step orders have always "ever been used with the latest technology"?

+3


source to share


1 answer


Thread.Sleep(1000)

is preferred because it is an idiomatic way to perform synchronous waits. It has been the standard for 15 years. There is nothing wrong.

Task.Delay(1000).Wait()

does the same. It's harder to understand, slower to type, slower to execute and, in my opinion, reverse thinking.



If you want a synchronous call to do a synchronous API for this. Sleep is not special in this regard.

+3


source







All Articles