如何在Windows服务中设置日期的计时器间隔 [英] how to set timer interval for day in windows service

查看:128
本文介绍了如何在Windows服务中设置日期的计时器间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Windows服务并将计时器间隔设置为24小时,当我启动服务时它每24小时触发一次事件,但现在我想在启动服务时首先触发事件,不需要等待24小时。



下面是代码:



protected override void OnStart(string [] args)

{

//在服务开始时将此行添加到文本文件

TraceService(启动服务);



//处理经过的事件

timer.Elapsed + = new ElapsedEventHandler(OnElapsedTime);



/ /此语句用于将间隔设置为1分钟(= 60,000毫秒)

// timer = new System.Timers.Timer();

timer.Interval = 60000 * 60 * 24;



//启用计时器

timer.Enabled = true;

计时器。 AutoReset = true;

timer.Start();

}

I created windows service and set the timer interval for 24 hours, when i start the service it triggered the event for every 24 hours, but now i want when start the service first event trigger immediately no need to wait for 24 hours.

below is the code:

protected override void OnStart(string[] args)
{
//add this line to text file during start of service
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 = new System.Timers.Timer();
timer.Interval = 60000*60*24;

//enabling the timer
timer.Enabled = true;
timer.AutoReset = true;
timer.Start();
}

推荐答案

简单的答案是:不要那样做!



还有一种更好的方法,就是设置一个较小间隔的定时器 - 多小取决于你的准确度需要与24小时间隔 - 如果您对在24小时到期后5分钟内发射的情况感到满意,则将间隔设置为5分钟。如果你需要它在适当的时间,然后设置为30秒。

然后,你记录事件的开火时间,并在每次定时器滴答时检查当前时间 - 如果当前时间晚于所需时间,重置开火时间并触发您的活动。



这样,事件不仅会在服务启动时触发,但如果您的服务因任何奇怪的原因没有获得Tick,则不能错过。
The simple answer is: "Don't do it like that!"

There is a much better way, which is to set a Timer for a smaller interval - how small depends on how accurate you need to be with the "24 hours" interval - if you are happy that it fires within 5 minutes of the 24 hours expiring, then set the interval to 5 minutes. If you need it in the right minute then set it to 30 seconds.
Then, you record a "fire time" with the event, and check it against the current time every time the Timer Ticks - if the current time is later than the required time, reset the fire time and trigger your event.

This way, the event will not only be fired when the service starts, but it can't be "missed" if your service doesn't get a Tick for any odd reason.


实际上,您需要的代码OnElapsedTime 将在Start开始执行,而不是立即触发的事件。所以只需调用该方法(或更好:该事件处理程序调用的方法),例如在 timer.Start 之后。

顺便说一下,拿走也是OriginalGriff的回答,或者跟随Mehdi Golam对你的另一个问题的回答:如何在Windows服务中设置每日的计时器间隔 [ ^ ](内部也使用30秒间隔)。
Actually, you want the code of OnElapsedTime to be executed at Start already, not the event to be fired immediately. So just call that method (or better: the method which that event handler calls), e.g after timer.Start.
By the way, take also OriginalGriff's answer into account, or follow Mehdi Golam's answer to your other question: how to set timer interval for daily in windows service[^] (which internally also uses a 30 seconds interval).


这篇关于如何在Windows服务中设置日期的计时器间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆