我想要一个5毫秒左右分辨率的定时器,但是.NET中现有的计时器只有大约50毫秒的分辨率。
虽然有些人声称可以在C#中实现,但我找不到任何可行的解决方案来创建高分辨率定时器。
我想要一个5毫秒左右分辨率的定时器,但是.NET中现有的计时器只有大约50毫秒的分辨率。
虽然有些人声称可以在C#中实现,但我找不到任何可行的解决方案来创建高分辨率定时器。
关于问题中特别询问的会在正常时间间隔触发事件的Timer
类的信息,我已经修改了此回答,在水平线以下是我的旧回答。
我使用Timer类测试了以下代码,在我的机器上看起来至少可以达到14-15毫秒的范围。请自行尝试并查看您是否能够重现此结果。因此,可以实现低于50毫秒的响应时间,但无法精确地达到一毫秒。
using System;
using System.Timers;
using System.Diagnostics;
public static class Test
{
public static void Main(String[] args)
{
Timer timer = new Timer();
timer.Interval = 1;
timer.Enabled = true;
Stopwatch sw = Stopwatch.StartNew();
long start = 0;
long end = sw.ElapsedMilliseconds;
timer.Elapsed += (o, e) =>
{
start = end;
end = sw.ElapsedMilliseconds;
Console.WriteLine("{0} milliseconds passed", end - start);
};
Console.ReadLine();
}
}
注意:以下是我的旧答案,当时我认为 OP 正在谈论计时事项。以下仅是关于计时事物的有用信息,但不能提供以固定时间间隔触发事件的方法。为此,需要使用 Timer 类。int resolution = 1E9 / Stopwatch.Frequency;
Console.WriteLine("The minimum measurable time on this system is: {0} nanoseconds", resolution);
如果你担心这个实际上是从哪里获取的,文档似乎暗示它实际上内部调用了更低级别的Win32函数:
Stopwatch类有助于在托管代码中操作与计时相关的性能计数器。具体来说,可以使用Frequency字段和GetTimestamp方法代替非托管的Win32 API QueryPerformanceFrequency和QueryPerformanceCounter。
这个网址怎么样?
public class HiResTimer
{
private bool isPerfCounterSupported = false;
private Int64 frequency = 0;
// Windows CE native library with QueryPerformanceCounter().
private const string lib = "coredll.dll";
[DllImport(lib)]
private static extern int QueryPerformanceCounter(ref Int64 count);
[DllImport(lib)]
private static extern int QueryPerformanceFrequency(ref Int64 frequency);
public HiResTimer()
{
// Query the high-resolution timer only if it is supported.
// A returned frequency of 1000 typically indicates that it is not
// supported and is emulated by the OS using the same value that is
// returned by Environment.TickCount.
// A return value of 0 indicates that the performance counter is
// not supported.
int returnVal = QueryPerformanceFrequency(ref frequency);
if (returnVal != 0 && frequency != 1000)
{
// The performance counter is supported.
isPerfCounterSupported = true;
}
else
{
// The performance counter is not supported. Use
// Environment.TickCount instead.
frequency = 1000;
}
}
public Int64 Frequency
{
get
{
return frequency;
}
}
public Int64 Value
{
get
{
Int64 tickCount = 0;
if (isPerfCounterSupported)
{
// Get the value here if the counter is supported.
QueryPerformanceCounter(ref tickCount);
return tickCount;
}
else
{
// Otherwise, use Environment.TickCount.
return (Int64)Environment.TickCount;
}
}
}
static void Main()
{
HiResTimer timer = new HiResTimer();
// This example shows how to use the high-resolution counter to
// time an operation.
// Get counter value before the operation starts.
Int64 counterAtStart = timer.Value;
// Perform an operation that takes a measureable amount of time.
for (int count = 0; count < 10000; count++)
{
count++;
count--;
}
// Get counter value when the operation ends.
Int64 counterAtEnd = timer.Value;
// Get time elapsed in tenths of a millisecond.
Int64 timeElapsedInTicks = counterAtEnd - counterAtStart;
Int64 timeElapseInTenthsOfMilliseconds =
(timeElapsedInTicks * 10000) / timer.Frequency;
MessageBox.Show("Time Spent in operation (tenths of ms) "
+ timeElapseInTenthsOfMilliseconds +
"\nCounter Value At Start: " + counterAtStart +
"\nCounter Value At End : " + counterAtEnd +
"\nCounter Frequency : " + timer.Frequency);
}
}
System.Diagnostics
中的 Stopwatch
类能够实现同样的功能。我不确定它们是否在 VS 2005 中提供了该类,但现在已经存在了,没有理由使用这种东西。 - Mike BaileyStopwatch
是一种更惯用的(更直接)实现相同效果的方法。我不会打负分,但我会告诫人们不要使用这种方法,而是转向使用Stopwatch
。 - Mike Bailey这是基于StopWatch计时器的实现
https://gist.github.com/DraTeots/436019368d32007284f8a12f1ba0f545它可以在所有平台上工作,并且在 StopWatch.IsHighPrecision == true
的情况下高精度。
其 Elapsed
事件保证不会重叠(这可能很重要,因为事件处理程序内的状态更改可能会受到多线程访问的保护)。
以下是使用方法:
Console.WriteLine($"IsHighResolution = {HighResolutionTimer.IsHighResolution}");
Console.WriteLine($"Tick time length = {HighResolutionTimer.TickLength} [ms]");
var timer = new HighResolutionTimer(0.5f);
// UseHighPriorityThread = true, sets the execution thread
// to ThreadPriority.Highest. It doesn't provide any precision gain
// in most of the cases and may do things worse for other threads.
// It is suggested to do some studies before leaving it true
timer.UseHighPriorityThread = false;
timer.Elapsed += (s, e) => { /*... e.Delay*/ }; // The call back with real delay info
timer.Start();
timer.Stop(); // by default Stop waits for thread.Join()
// which, if called not from Elapsed subscribers,
// would mean that all Elapsed subscribers
// are finished when the Stop function exits
timer.Stop(joinThread:false) // Use if you don't care and don't want to wait
虽然晚了点但还是可能对寻找答案的人有所帮助,因为这个话题已经十年没有变化了。
.NET中的任何延迟操作都会涉及系统时钟分辨率,即使用timeBeginPeriod()设置的分辨率。无论是Thread.Sleep(N)、Threading.Timer还是Waitable.WaitOne(N),都是如此。而DateTime.Now()和System.Diagnostic.Stopwatch的时间分辨率要高得多,因此有一种实现精确计时事件的方法称为hot loop。但是,由于这些循环倾向于占用处理器核心,因此易受操作系统的严重威胁。以下是我们防止这种情况发生的方法:
在热循环中不再需要线程时间量时,通过调用Thread.Sleep(0)或.WaitOne(0)将线程时间量让给其他线程
下面是一个展示高分辨率调度程序简单实现的代码片段:
注:本文中的“hot loop”直译为热循环,但是建议保留原文中的术语以便更好地理解。
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// High resolution scheduler.
/// License: public domain (no restrictions or obligations)
/// Author: Vitaly Vinogradov
/// </summary>
public class HiResScheduler : IDisposable
{
/// <summary>
/// Scheduler would automatically downgrade itself to cold loop (Sleep(1)) when there are no
/// tasks earlier than the treshold.
/// </summary>
public const int HOT_LOOP_TRESHOLD_MS = 16;
protected class Subscriber : IComparable<Subscriber>, IComparable
{
public Action Callback { get; set; }
public double DelayMs { get; set; }
public Subscriber(double delay, Action callback)
{
DelayMs = delay;
Callback = callback;
}
public int CompareTo(Subscriber other)
{
return DelayMs.CompareTo(other.DelayMs);
}
public int CompareTo(object obj)
{
if (ReferenceEquals(obj, null))
return -1;
var other = obj as Subscriber;
if (ReferenceEquals(other, null))
return -1;
return CompareTo(other);
}
}
private Thread _spinner;
private ManualResetEvent _allowed = new ManualResetEvent(false);
private AutoResetEvent _wakeFromColdLoop = new AutoResetEvent(false);
private bool _disposing = false;
private bool _adding = false;
private List<Subscriber> _subscribers = new List<Subscriber>();
private List<Subscriber> _pendingSubscribers = new List<Subscriber>();
public bool IsActive { get { return _allowed.WaitOne(0); } }
public HiResScheduler()
{
_spinner = new Thread(DoSpin);
_spinner.Start();
}
public void Start()
{
_allowed.Set();
}
public void Pause()
{
_allowed.Reset();
}
public void Enqueue(double delayMs, Action callback)
{
lock (_pendingSubscribers)
{
_pendingSubscribers.Add(new Subscriber(delayMs, callback));
_adding = true;
if (delayMs <= HOT_LOOP_TRESHOLD_MS * 2)
_wakeFromColdLoop.Set();
}
}
private void DoSpin(object obj)
{
var sw = new Stopwatch();
sw.Start();
var nextFire = null as Subscriber;
while (!_disposing)
{
_allowed.WaitOne();
if (nextFire != null && sw.Elapsed.TotalMilliseconds >= nextFire?.DelayMs)
{
var diff = sw.Elapsed.TotalMilliseconds;
sw.Restart();
foreach (var item in _subscribers)
item.DelayMs -= diff;
foreach (var item in _subscribers.Where(p => p.DelayMs <= 0).ToList())
{
item.Callback?.Invoke();
_subscribers.Remove(item);
}
nextFire = _subscribers.FirstOrDefault();
}
if (_adding)
lock (_pendingSubscribers)
{
_subscribers.AddRange(_pendingSubscribers);
_pendingSubscribers.Clear();
_subscribers.Sort();
_adding = false;
nextFire = _subscribers.FirstOrDefault();
}
if (nextFire == null || nextFire.DelayMs > HOT_LOOP_TRESHOLD_MS)
_wakeFromColdLoop.WaitOne(1);
else
_wakeFromColdLoop.WaitOne(0);
}
}
public void Dispose()
{
_disposing = true;
}
}
private static Int64 m_iPerfFrequency = -1;
public static double GetPerfCounter()
{
// see if we need to get the frequency
if (m_iPerfFrequency < 0)
{
if (QueryPerformanceFrequency(out m_iPerfFrequency) == 0)
{
return 0.0;
}
}
Int64 iCount = 0;
if (QueryPerformanceCounter(out iCount) == 0)
{
return 0.0;
}
return (double)iCount / (double)m_iPerfFrequency;
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int QueryPerformanceCounter(out Int64 iCount);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int QueryPerformanceFrequency(out Int64 iFrequency);
此函数以秒为单位返回性能计数器。您使用性能计时器的原因是与传统的C++代码共享计时器,或获得比C# StopWatch类更精确的计时器。
您可以使用本文中概述的QueryPerformanceCounter()
和QueryPerformanceTimer()
。
Timer
类的文档,关于间隔属性的部分:http://msdn.microsoft.com/en-us/library/system.timers.timer.interval.aspx。它似乎可以精确到毫秒级别,只需指定一个 1 的间隔即可。当你设置这个值时,它是否仍然比平常慢得多? - Mike Bailey