Problems with using Thread.Sleep for short times

前端 未结 5 886
执笔经年
执笔经年 2020-12-11 06:42

I have an app with 2 threads (now), but it seems that function Thread.Sleep() doesn\'t work very good. It sleeps threads but it takes much more time (for example- I want to

5条回答
  •  一个人的身影
    2020-12-11 07:13

    As others have indicated, the default resolution of Sleep is 10 or 15 milliseconds, depending on the edition of Windows.

    However, you can reprogram the timer to use a 1 millisecond resolution by issuing a

    timeBeginPeriod(1);
    timeEndPeriod(1);
    

    where

    [DllImport(WINMM)]
    internal static extern uint timeBeginPeriod(uint period);   
    

    We do this in our serial communications services where being able to accurately space out sends in time is important. Some people are reluctant to do this because it causes Windows to do other things that are based off of the timer more frequently as well. In reality this has caused no discernible issues for us, and we have hundreds of installs each with hundreds of serial devices connected.

提交回复
热议问题