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

前端 未结 5 672
礼貌的吻别
礼貌的吻别 2020-11-22 07:54

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
5条回答
  •  臣服心动
    2020-11-22 08:33

    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.

提交回复
热议问题