.NET Thread.Sleep() is randomly imprecise

前端 未结 7 1895
故里飘歌
故里飘歌 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 02:06

    Thread.Sleep() is not precise. The number you give it is the MINIMUM time to sleep, not the exact time to sleep. Further, Thread.Sleep also has a minimum granularity, which I think Is around 10ms IIRC.

    If you need more precise timing, then you probably want to use a timer instead. However, be aware that Windows and .NET in particular are not "Real Time", so they cannot give you pinpoint accuracy.

    For example, if the garbage collector kicks in, it may take longer to process timers.

提交回复
热议问题