Why Thread.Sleep waits longer than requested when other apps are running?

后端 未结 5 463
既然无缘
既然无缘 2020-12-07 02:53

I have a small problem regarding threading in C#. For some reason, my thread speeds up from 32ms delay to 16ms delay when I open Chrome, when I close Chrome it goes back to

5条回答
  •  一生所求
    2020-12-07 03:11

    You are hitting a resolution issue with DateTime. You should use Stopwatch for this kind of precision. Eric Lippert states that DateTime is only accurate to around 30 ms, so your readings with it in this case will not tell you anything.

    Measurement is half of your problem. The actual time variation for your loop is due to Sleep resolution (as stated in the other answers).

提交回复
热议问题