When to use Task.Delay, when to use Thread.Sleep in C#?
Use
Thread.Sleep
when you want to block the current thread.Use
Task.Delay
when you want a logical delay without blocking the current thread.Conculsion
The biggest difference between
Task.Delay
and Thread.Sleep
is that Task.Delay
is intended to run asynchronously. It does not make sense to use Task.Delay
in synchronous code. It is a VERY bad idea to use Thread.Sleep
in asynchronous code.