小编Rap*_*ael的帖子

c#计时器不准确?

我有一个功能,应该在一个特定的间隔后发送一个信号,精确到1毫秒(ms).但似乎我的计时器需要的时间比他应该的长,即我将功能传递给TimeSpan 20ms,但计时器需要每个刻度30ms.我现在自己写了一个计时器,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!");
}
Run Code Online (Sandbox Code Playgroud)

在我的代码中,计时器唯一执行的是waitForTimerReset.Set()允许线程在被a停止后继续运行的方法ManualResetEvent,这意味着此调用不应该花费10毫秒.

c# timer visual-studio-2013

2
推荐指数
1
解决办法
826
查看次数

标签 统计

c# ×1

timer ×1

visual-studio-2013 ×1