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

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

    if the current thread is killed and you use Thread.Sleep and it is executing then you might get a ThreadAbortException. With Task.Delay you can always provide a cancellation token and gracefully kill it. Thats one reason I would choose Task.Delay. see http://social.technet.microsoft.com/wiki/contents/articles/21177.visual-c-thread-sleep-vs-task-delay.aspx

    I also agree efficiency is not paramount in this case.

提交回复
热议问题