System.Timers.Timer设置为24小时间隔

本文关键字:小时间 设置 Timers Timer System | 更新日期: 2024-10-18 20:10:42

设置Interval属性以确保计时器以每天24小时的间隔启动是否正确

this.NotificationTimer = new System.Timers.Timer();
this.NotificationTimer.Interval = 86400000D;

我将24小时转换为86400000毫秒。请确认这是否是正确的方法/值。否则,解释原因。

System.Timers.Timer设置为24小时间隔

Interval属性获取或设置引发Elapsed事件的间隔(以毫秒为单位)。所以你所做的是正确的。请参阅MSDN。

使用Timer构造函数更优化,可能更容易阅读

this.NotificationTimer = new System.Timers.Timer(86400000D);

在计算以毫秒为单位的正确时间方面,可以调用TimeSpan方法。这提高了可读性;缺点是执行速度稍慢:

this.NotificationTimer = new System.Timers.Timer(TimeSpan.FromHours(24).TotalMilliseconds);