尽管timeBeginPeriod(1),为什么最小Threading.Timer间隔15ms

Eug*_*sky 3 .net sleep timer

以下代码段

    [DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")]
    public static extern uint TimeBeginPeriod(uint uMilliseconds);

    static void Main(string[] args)
    {
        if (TimeBeginPeriod(1) != 0)
            Console.WriteLine("TimeBeginPeriod failed!");

        Console.WriteLine("Sleep");
        Stopwatch sw = Stopwatch.StartNew();
        for (int i = 0; i < 10; i++)
        {
            Thread.Sleep(1);
            Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency);
            sw.Restart();
        }

        Console.WriteLine("Threading.Timer");
        sw = null;
        System.Threading.Timer t = null;
        int n = 0;

        t = new Timer(state =>
        {
            if (sw == null)
                sw = Stopwatch.StartNew();
            else
            {
                Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency);
                n++;
                sw.Restart();
            }
            if (n == 10)
                t.Change(Timeout.Infinite, Timeout.Infinite);
        }, null, TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(1));
        Console.ReadKey();
    }
Run Code Online (Sandbox Code Playgroud)

将产生例如此输出:

Sleep
0.151834939915548
0.757358826331279
0.786901687225611
0.712520725399457
0.715593741662697
0.798704863327602
0.5724889615859
0.648825479215934
0.436927039609783
0.517873081634677
Threading.Timer
15.5841035662354
14.8620145856526
15.1098812837944
14.4202684978119
15.3883384620112
14.7210748852159
15.307462261265
15.7125416777831
14.5991320125882
15.6035194417168
Run Code Online (Sandbox Code Playgroud)

根据网络,例如Hans Passant评论,timeBeginPeriod影响了常规(.net)计时器.那么为什么我的计时器仍然具有这种粗粒度?Thread.Sleep似乎做得很好.

也许相关:它运行在VMWare内的Windows 7,64位,.net 4上.

Jim*_*hel 5

评论错了.我的经验是多媒体计时器不会影响.NET计时器.也就是说,它不会改变它们的最小支持时间段,这似乎是大约15毫秒.它可能会提高准确性.也就是说,如果你要求16毫秒,你可能实际上得到16毫秒而不是"在15到30毫秒之间".

为什么.NET定时器限制为15 ms我不清楚.

这里接受的答案中有一些关于它的信息.

如果您正在寻找更高分辨率的.NET计时器,您可能不应该使用多媒体计时器.这些已在Windows API中弃用.使用计时器队列计时器.看我的文章:

另一种选择是使用Waitable Timer.有关该文件的完整资料,请访问http://www.mischel.com/pubs/waitabletimer.zip