【发布时间】:2017-04-10 17:44:41
【问题描述】:
我有一个函数应该在特定间隔后发送信号,精确到 1 毫秒 (ms)。
但似乎我的计时器需要的时间比他应该的要长一些,即我将一个 TimeSpan 传递给函数,时间为 20 毫秒,但计时器每次滴答需要 30 毫秒。我现在自己用Stopwatch 编写了一个计时器,但我仍然想知道为什么计时器需要更多时间来执行?
private System.Timers.Timer timer;
private void startResetTimer(TimeSpan time)
{
timer = new System.Timers.Timer(time.TotalMilliseconds);
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
if (!waitForTimerReset.Set())
Console.WriteLine("Could not wake up any threads!");
}
在我的代码中,计时器唯一执行的是waitForTimerReset.Set() 方法,它允许线程在被ManualResetEvent 停止后继续,这意味着此调用不应花费 10 毫秒。
【问题讨论】:
标签: c# visual-studio-2013 timer