.net - 为什么 .NET 计时器的分辨率限制为 15 毫秒?

标签 .net timer

请注意,我问的是使用 System.Threading.Timer 之类的东西调用回调函数的频率超过每 15 毫秒一次的东西。我不是在问如何使用 System.Diagnostics.Stopwatch 或什至 QueryPerformanceCounter 来准确计时一段代码。

此外,我已经阅读了相关问题:

Accurate Windows timer? System.Timers.Timer() is limited to 15 msec

High resolution timer in .NET

两者都不能为我的问题提供有用的答案。

另外,推荐MSDN文章,Implement a Continuously Updating, High-Resolution Time Provider for Windows ,是关于计时而不是提供连续的滴答流。

话虽如此。 . .

有关 .NET 计时器对象的信息有很多。例如,System.Timers.Timer 被宣传为“针对服务器应用程序优化的高性能计时器”。而 System.Threading.Timer 在某种程度上被认为是二等公民。传统观点认为 System.Threading.Timer 是 Windows 的包装器 Timer Queue Timers System.Timers.Timer 完全是另一回事。

现实情况大不相同。 System.Timers.Timer 只是 System.Threading.Timer 的一个薄组件包装器(只需使用 Reflector 或 ILDASM 来查看 System.Timers.Timer 内部) code> 并且您会看到对 System.Threading.Timer 的引用),并且有一些代码将提供自动线程同步,因此您不必这样做。

System.Threading.Timer,事实证明不是计时器队列计时器的包装器。至少在从 .NET 2.0 到 .NET 3.5 的 2.0 运行时中没有。使用 Shared Source CLI 的几分钟显示运行时实现了自己的计时器队列,类似于 Timer Queue Timers,但实际上从未调用 Win32 函数。

看来 .NET 4.0 运行时也实现了自己的计时器队列。我的测试程序(见下文)在 .NET 4.0 下提供了与在 .NET 3.5 下类似的结果。我已经为 Timer Queue Timers 创建了自己的托管包装器,并证明我可以获得 1 ms 的分辨率(具有相当高的准确性),因此我认为我读取 CLI 源代码的可能性不大。

我有两个问题:

首先,是什么原因导致运行时对定时器队列的实现如此缓慢?我无法获得超过 15 毫秒的分辨率,而且精度似乎在 -1 到 +30 毫秒的范围内。也就是说,如果我要求 24 毫秒,我会得到相隔 23 到 54 毫秒的滴答声。我想我可以花更多时间使用 CLI 源代码来找到答案,但我认为这里有人可能知道。

其次,我意识到这很难回答,为什么不使用 Timer Queue Timers?我意识到 .NET 1.x 必须在没有这些 API 的 Win9x 上运行,但它们自 Windows 2000 以来就已经存在,如果我没记错的话这是 .NET 2.0 的最低要求。是因为 CLI 必须在非 Windows 机器上运行吗?

我的定时器测试程序:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace TimerTest
{
    class Program
    {
        const int TickFrequency = 5;
        const int TestDuration = 15000;   // 15 seconds

        static void Main(string[] args)
        {
            // Create a list to hold the tick times
            // The list is pre-allocated to prevent list resizing
            // from slowing down the test.
            List<double> tickTimes = new List<double>(2 * TestDuration / TickFrequency);

            // Start a stopwatch so we can keep track of how long this takes.
            Stopwatch Elapsed = Stopwatch.StartNew();

            // Create a timer that saves the elapsed time at each tick
            Timer ticker = new Timer((s) =>
                {
                    tickTimes.Add(Elapsed.ElapsedMilliseconds);
                }, null, 0, TickFrequency);

            // Wait for the test to complete
            Thread.Sleep(TestDuration);

            // Destroy the timer and stop the stopwatch
            ticker.Dispose();
            Elapsed.Stop();

            // Now let's analyze the results
            Console.WriteLine("{0:N0} ticks in {1:N0} milliseconds", tickTimes.Count, Elapsed.ElapsedMilliseconds);
            Console.WriteLine("Average tick frequency = {0:N2} ms", (double)Elapsed.ElapsedMilliseconds / tickTimes.Count);

            // Compute min and max deviation from requested frequency
            double minDiff = double.MaxValue;
            double maxDiff = double.MinValue;
            for (int i = 1; i < tickTimes.Count; ++i)
            {
                double diff = (tickTimes[i] - tickTimes[i - 1]) - TickFrequency;
                minDiff = Math.Min(diff, minDiff);
                maxDiff = Math.Max(diff, maxDiff);
            }

            Console.WriteLine("min diff = {0:N4} ms", minDiff);
            Console.WriteLine("max diff = {0:N4} ms", maxDiff);

            Console.WriteLine("Test complete.  Press Enter.");
            Console.ReadLine();
        }
    }
}

最佳答案

也许此处链接的文档对此做了一些解释。它有点枯燥,所以我只是快速浏览了一下:)

引用介绍:

The system timer resolution determines how frequently Windows performs two main actions:

  • Update the timer tick count if a full tick has elapsed.
  • Check whether a scheduled timer object has expired.

A timer tick is a notion of elapsed time that Windows uses to track the time of day and thread quantum times. By default, the clock interrupt and timer tick are the same, but Windows or an application can change the clock interrupt period.

The default timer resolution on Windows 7 is 15.6 milliseconds (ms). Some applications reduce this to 1 ms, which reduces the battery run time on mobile systems by as much as 25 percent.

最初来自:Timers, Timer Resolution, and Development of Efficient Code (docx).

关于.net - 为什么 .NET 计时器的分辨率限制为 15 毫秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7203024/

相关文章:

c# - 免费的 .NET 图表控件(如 Nevron Diagrams)

python - 相当于 Python Numpy 函数的 F# 库或 .Net Numerics

.net - 使用C#从Active Directory获取组织单位

vb.net - 如何让标签闪烁

java - 为什么 EJB 定时器服务使用 Singleton 作为标准?

c# - 帮助 string.format - 构建 URL

.net - 如何尽快将大量记录插入 MySql 数据库

java - 如何在Android中正确实现定时器?

xcode - 执行某项操作 "x"秒,然后执行其他操作

c - POSIX 计时器和 POSIX 信号处理