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

前端 未结 5 744
礼貌的吻别
礼貌的吻别 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:44

    I want to add something. Actually, Task.Delay is a timer based wait mechanism. If you look at the source you would find a reference to a Timer class which is responsible for the delay. On the other hand Thread.Sleep actually makes current thread to sleep, that way you are just blocking and wasting one thread. In async programming model you should always use Task.Delay() if you want something(continuation) happen after some delay.

提交回复
热议问题