简体   繁体   中英

Timer with two intervals for execution of tasks

I have ac# windows service with a timer that executes a process every 5 minutes.

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? 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. Also, creating a new (winform) timer does not create a new thread as far as I know.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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