When to use Task.Delay, when to use Thread.Sleep?

Tom K. picture Tom K. · Nov 19, 2013 · Viewed 254.6k times · Source

Are there good rule(s) for when to use Task.Delay versus Thread.Sleep?

  • Specifically, is there a minimum value to provide for one to be effective/efficient over the other?
  • Lastly, since Task.Delay causes context-switching on a async/await state machine, is there an overhead of using it?

Answer

Stephen Cleary picture Stephen Cleary · Nov 20, 2013

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.

Efficiency should not be a paramount concern with these methods. Their primary real-world use is as retry timers for I/O operations, which are on the order of seconds rather than milliseconds.