C#,System.Timers.Timer,每分钟与系统时钟同步运行

标签 c# timer system synchronization clock

我正在尝试让计时器每分钟与系统时钟(00:01:00、00:02:00、00:03:00 等)同步运行。下面的代码会产生一个稳定的定时器吗?我试过运行它。计时器每分钟精确运行一次。但问题是,它会在几个月后开始消失吗?

private System.Timers.Timer timer;

public frmMain()
{
            timer = new System.Timers.Timer();
            timer.AutoReset = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Interval = GetInterval();
            timer.Start();
}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
            //Do processing

            timer.Interval = GetInterval();
            timer.Start();

}
private double GetInterval()
{
            DateTime now = DateTime.Now;
            return ((60 - now.Second) * 1000 - now.Millisecond);
}

最佳答案

不会。

DateTime.Now 使用系统时钟作为引用计时器,因此您的计时器将始终与其保持同步,以系统时钟分辨率的几毫秒为模数。

关于C#,System.Timers.Timer,每分钟与系统时钟同步运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014070/

相关文章:

c# - DotNetOpenAuth:何时调用 StoreNewRequestToken?

c# - 通过Timer控制函数调用c#

C#:GPS 跟踪系统

c# - 如何将服务添加到 .Net 中属性网格的类型描述符上下文?

c# - 如何在 Xamarin Forms 中设置条目的顶部填充

javascript - 如何取消 JavaScript 对话?

javascript - setTimeout(code,millisec) api 的替代方案

c# - System.Threading.Tasks 在 Visual Studio 2010 Express 中可用吗?

c# - 如何获取实际(本地化)的文件夹名称?

c# - 无法在 IGraphBuilder.RenderFile 中使用 COM 异常播放 MP3 文件