简体   繁体   English

具有两个间隔的计时器,用于执行任务

[英]Timer with two intervals for execution of tasks

I have ac# windows service with a timer that executes a process every 5 minutes. 我有一个带有计时器的ac#Windows服务,该计时器每5分钟执行一次处理。

I don't want to create other process to execute a task every 30 minutes, so is it possible within the same timer timer_Elapsed event to do that logic? 我不想创建其他进程每30分钟执行一次任务,因此是否可以在同一计时器timer_Elapsed事件中执行该逻辑? any clue? 有什么线索吗?

Thanks a lot, 非常感谢,

如何使用2个计时器?

You could increment a variable every time the timer elapses, and when that variable reaches 6, you reset it to 0 and execute your 'once every 30 minute' code. 您可以在每次计时器过去时增加一个变量,当该变量达到6时,将其重置为0并执行“每30分钟一次”的代码。 Also, creating a new (winform) timer does not create a new thread as far as I know. 而且,据我所知,创建新的(winform)计时器不会创建新的线程。

int TimerVariable=0;
TimerEvent(object sender,eventargs e)
{
    TimerVariable++;
    if(TimverVariable>=6)
    {
        //Execute the once every 30 min code
    }
    //Execute the once every 5 min code.
}

Well, you could just use a counter and check it against 6 (30000/5000) and reset it back to zero when done. 好吧,您只需要使用一个计数器,然后将其检查为6 (30000/5000),然后将其重置为零即可。

private int counter = 0;
private void TimerCallback()
{
    counter++;
    if(counter >=6)
    {
        //Your 30 seconds code here
        counter = 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM