HiveBrain v1.2.0
Get Started
← Back to all entries
principlecsharpCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
usesleepwhendelaythreadtask

Problem

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?

Solution

Use Thread.Sleep when you want to block the current thread.

Use await 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.

Context

Stack Overflow Q#20082221, score: 661

Revisions (0)

No revisions yet.