c# - System.Threading.Timer 与 System.Threading.Thread.Sleep 解析 - .NET Timer 不使用系统时钟解析

标签 c# .net multithreading timer scheduling

问题: 为什么 System.Threading.Timer 保持 15 毫秒的分辨率,尽管操作系统时钟分辨率要精确得多? 在没有繁忙的 CPU 等待的情况下实现 1ms 计时事件分辨率的推荐方法是什么?

再次强调:系统计时器在我的案例中有 1ms 分辨率(与建议的重复问题相反)。所以这不是系统计时器解析的问题。因此,在所谓的重复问题中没有有用的信息。

背景: 似乎 .NET System.Threading.Timer 没有使用系统时钟分辨率 - 它保持约 15 毫秒的分辨率。尽管操作系统时钟(例如 Sleep 分辨率)要精确得多。

在我的机器上(当几乎闲置且有 4 个内核可运行时):

>Clockres.exe

ClockRes v2.0 - View the system clock resolution
Copyright (C) 2009 Mark Russinovich
SysInternals - www.sysinternals.com

Maximum timer interval: 15.625 ms
Minimum timer interval: 0.500 ms
Current timer interval: 1.001 ms

我的快速测试的输出:

Sleep test:
Average time delta: 2[ms] (from 993 cases)
System.Threading.Timer test:
Average time delta: 15[ms] (from 985 cases)

测试代码所在位置:

private static void TestSleepVsTimer(long millisecondsDifference, int repetions)
{
    TimingEventsKeeper timingEventsKeeper = new TimingEventsKeeper();
    timingEventsKeeper.Reset((int) millisecondsDifference, repetions);

    while (!timingEventsKeeper.TestDoneEvent.IsSet)
    {
        timingEventsKeeper.CountNextEvent(null);
        Thread.Sleep((int) millisecondsDifference);
    }

    Console.WriteLine("Sleep test: ");
    timingEventsKeeper.Output();

    timingEventsKeeper.Reset((int) millisecondsDifference, repetions);

    Timer t = new Timer(timingEventsKeeper.CountNextEvent, null, TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(1));
    timingEventsKeeper.TestDoneEvent.Wait();

    Console.WriteLine("System.Threading.Timer test: ");
    timingEventsKeeper.Output();
}

private class TimingEventsKeeper
{
    long _ticksSum = 0;
    long _casesCount = 0;
    long _minTicksDiff;
    long _maxTicksDiff;
    long _lastTicksCount;
    int _repetitons;

    public CountdownEvent TestDoneEvent = new CountdownEvent(0);

    public void Reset(int millisecondsDifference, int repetitions)
    {
        _ticksSum = 0;
        _casesCount = 0;
        _minTicksDiff = millisecondsDifference * 10000;
        _maxTicksDiff = millisecondsDifference * 10000;
        _lastTicksCount = DateTime.UtcNow.Ticks;
        _repetitons = repetitions;
        TestDoneEvent.Reset(repetitions);
    }

    public void CountNextEvent(object unused)
    {
        long currTicksCount = DateTime.UtcNow.Ticks;
        long diff = currTicksCount - _lastTicksCount;
        _lastTicksCount = currTicksCount;

        TestDoneEvent.Signal();

        if (diff >= _maxTicksDiff)
        {
            _maxTicksDiff = diff;
            return;
        }

        if (diff <= _minTicksDiff)
        {
            _minTicksDiff = diff;
            return;
        }

        _casesCount++;
        _ticksSum += diff;

    }

    public void Output()
    {
        if(_casesCount > 0)
            Console.WriteLine("Average time delta: {0}[ms] (from {1} cases)", _ticksSum / _casesCount / 10000, _casesCount);
        else
            Console.WriteLine("No measured cases to calculate average");
    }
}

public static class WinApi
{
    /// <summary>TimeBeginPeriod(). See the Windows API documentation for details.</summary>

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
    [DllImport("winmm.dll", EntryPoint = "timeBeginPeriod", SetLastError = true)]

    public static extern uint TimeBeginPeriod(uint uMilliseconds);

    /// <summary>TimeEndPeriod(). See the Windows API documentation for details.</summary>

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
    [DllImport("winmm.dll", EntryPoint = "timeEndPeriod", SetLastError = true)]

    public static extern uint TimeEndPeriod(uint uMilliseconds);
}

private static void Main(string[] args)
{
    WinApi.TimeBeginPeriod(1);
    TestSleepVsTimer(1, 1000);
    WinApi.TimeEndPeriod(1);
}

编辑 1:

环境: 在 .NET 2.0、3.0、3.5(无 CountDownEvent)和 4.5 下的构建和发布版本上进行测试 在 Windows 8(内部版本 9200)、Server 2012(内部版本 9200)、Server 2008(内部版本 6001 SP1)上 SleepTimer 之间存在显着差异。

为什么这不是重复的: 正如我发布的那样 - 操作系统计时器分辨率设置为 1 毫秒(并且 Sleep 也没有表现出这种行为)。 因此,这不是操作系统计时器分辨率(中断频率)的错误 - 这是 System.Threading.Timer 特有的问题。

编辑 2: (向代码添加了 TimeBeginPeriodTimeEndPeriod 调用 - 以强制更改操作系统计时器分辨率)

最佳答案

使用派生自 WaitHandle 的同步类之一,例如 AutoResetEvent 或 ManualResetEvent,在调用 WaitOne() 方法时设置超时参数。

通过在循环中调用 WaitOne,您可以实现一个计时器。

您可以向等待句柄派生类发出信号以中断或中断计时器。

请注意,要更改分辨率,最好使用实现 IDisposable 的辅助类:

internal sealed class TimePeriod : IDisposable
{
    private const string WINMM = "winmm.dll";

    private static TIMECAPS timeCapabilities;

    private static int inTimePeriod;

    private readonly int period;

    private int disposed;

    [DllImport(WINMM, ExactSpelling = true)]
    private static extern int timeGetDevCaps(ref TIMECAPS ptc, int cbtc);

    [DllImport(WINMM, ExactSpelling = true)]
    private static extern int timeBeginPeriod(int uPeriod);

    [DllImport(WINMM, ExactSpelling = true)]
    private static extern int timeEndPeriod(int uPeriod);

    static TimePeriod()
    {
        int result = timeGetDevCaps(ref timeCapabilities, Marshal.SizeOf(typeof(TIMECAPS)));
        if (result != 0)
        {
            throw new InvalidOperationException("The request to get time capabilities was not completed because an unexpected error with code " + result + " occured.");
        }
    }

    internal TimePeriod(int period)
    {
        if (Interlocked.Increment(ref inTimePeriod) != 1)
        {
            Interlocked.Decrement(ref inTimePeriod);
            throw new NotSupportedException("The process is already within a time period. Nested time periods are not supported.");
        }

        if (period < timeCapabilities.wPeriodMin || period > timeCapabilities.wPeriodMax)
        {
            throw new ArgumentOutOfRangeException("period", "The request to begin a time period was not completed because the resolution specified is out of range.");
        }

        int result = timeBeginPeriod(period);
        if (result != 0)
        {
            throw new InvalidOperationException("The request to begin a time period was not completed because an unexpected error with code " + result + " occured.");
        }

        this.period = period;
    }

    internal static int MinimumPeriod
    {
        get
        {
            return timeCapabilities.wPeriodMin;
        }
    }

    internal static int MaximumPeriod
    {
        get
        {
            return timeCapabilities.wPeriodMax;
        }
    }

    internal int Period
    {
        get
        {
            if (this.disposed > 0)
            {
                throw new ObjectDisposedException("The time period instance has been disposed.");
            }

            return this.period;
        }
    }

    public void Dispose()
    {
        if (Interlocked.Increment(ref this.disposed) == 1)
        {
            timeEndPeriod(this.period);
            Interlocked.Decrement(ref inTimePeriod);
        }
        else
        {
            Interlocked.Decrement(ref this.disposed);
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct TIMECAPS
    {
        internal int wPeriodMin;

        internal int wPeriodMax;
    }
}

然后您可以使用:

using (new TimePeriod(1))
{
    ////...
}

尼克

关于c# - System.Threading.Timer 与 System.Threading.Thread.Sleep 解析 - .NET Timer 不使用系统时钟解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23215970/

相关文章:

c# - 带参数的 SqlCommand

.net - ASP.Net MVC 中的 JsonResult 又名 Json 函数中的控制序列化器?

c# - 复合 linq 查询

c# - 链接 Task.WhenAll()

c# - 如何不触发 comboBox.SelectedIndexChange 并更新它?

C# BackgroundWorker 循环回问题

java - 多客户端聊天程序,向所有客户端广播聊天?

c# - 由于挂起的数据库调用而取消线程

c# - 如何确定另一个应用程序的窗口是否可见?

c# - 如何在 C# 中使用 XMLREADER 从 XML 字符串中读取特定元素