c# - 如何在Windows服务中设置一天的定时器间隔

标签 c# timer

我创建了一个 Windows 服务。我在其中设置了每隔一分钟的计时器间隔,因此它每分钟触发一次。但是我每天都需要触发相同的东西..

您可以在下面找到我在 OnStart() 方法中设置计时器间隔的代码。

代码:

    protected override void OnStart(string[] args)
    {
        TraceService("start service");

        //handle Elapsed event
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

        //This statement is used to set interval to 1 minute (= 60,000 milliseconds)

        timer.Interval = 86400000;

        //enabling the timer
        timer.Enabled = true;
    }

最佳答案

这篇文章很老,但我认为值得一提的是,与其使用像 86400000 这样的魔数(Magic Number),不如使用像 new TimeSpan(1, 0, 0, 0).TotalMilliseconds 这样的东西,这样如果有人需要改变他们会知道应该改变什么。

关于c# - 如何在Windows服务中设置一天的定时器间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18503712/

相关文章:

Visual Studio 2010 Express 中忽略了 C# 预处理器指令?

c# - 使用 LINQ 在 C# 中对列表进行排序

c# - 如何将 boolean 值绑定(bind)到组合框

c# - 更精确的 Thread.Sleep

objective-c - 计算两个时间点之间的秒数,在 Cocoa 中,即使系统时钟在中途发生变化

c# - 计时器之谜 - Forms.Timer 与 Threading.Timer

javascript - JS : Timer function with dynamic interval getting "off" by one second(out of sync)?

java - 如何修复JAVA中的 "The constructor Timer(int, new ActionListener(){}) is undefined"错误?

c# - 使用 javascript 确认框

java - 我的计时器在 java 中随机停止