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

后端 未结 5 469
既然无缘
既然无缘 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:19

    Basically, Thread.Sleep isn't very accurate.

    Thread.Sleep(1000/60) (which evaluates to Thread.Sleep(16)), asks the thread to go to sleep and come back when 16ms has elapsed. However, that thread might not get to execute again until a greater amount of time has elapsed; say, for example, 32ms.

    As for why Chrome is having an effect, I don't know but since Chrome spawns one new thread for each tab, it'll have an effect on the system's threading behaviour.

提交回复
热议问题