.NET Thread.Sleep() is randomly imprecise

前端 未结 7 1901
故里飘歌
故里飘歌 2021-01-20 01:34

In my .NET application I have to replay a series of sensor events. So I created a thread that fires these events (usually about every 1 - 4 millisecond). I implemented a loo

7条回答
  •  孤街浪徒
    2021-01-20 01:59

    You're not the only one to point this out recently. Something must have changed since XP. Sleep() was then pretty reliable, and indeed accurate, over intervals where it's useful, eg. seconds or minutes. 4ms is totally outside of what is available via Sleep(), as others have pointed out.

    It seems that now, with later OS, some extra uncertainty has crept in somehow, though I haven't tested it myself.

    Over usefully long periods, you should raise the priority of the sleeping thread so it will get dispatched as soon as it is made ready by the expiry of the sleep interval.

    For 4ms, look at multimedia timers, as suggested by Science Fiction, (+1).

提交回复
热议问题