geoNeo_ geoNeo_ - 1 month ago 12
C# Question

What is the equivalent of a timer tick of 30 minutes?

I have a timer tick that I would like to kick off my backgroundworker every 30 minutes. What is the equivalent value of 30 minutes for a timer tick?

Below follows the code:

_timer.Tick += new EventHandler(_timer_Tick);
_timer.Interval = (1000) * (1);
_timer.Enabled = true;
_timer.Start();

void _timer_Tick(object sender, EventArgs e)
{
_ticks++;
if (_ticks == 15)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}

_ticks = 0;
}
}


I am not sure if this is the best way or if anyone has a better suggestion.

RB. RB.
Answer

The Interval property of a timer is specified in milliseconds, not ticks.

Therefore, for a timer which fires every 30 minutes, simply do:

// 1000 is the number of milliseconds in a second.
// 60 is the number of seconds in a minute
// 30 is the number of minutes.
_timer.Interval = 1000 * 60 * 30;

However, I'm not clear what the Tick event you use is. I think you mean Elapsed?

EDIT As CodeNaked makes clear, you are talking about the System.Windows.Forms.Timer, not the System.Timers.Timer. Luckily, my answer applies to both :)

Finally, I don't understand why you maintain a count (_ticks) within your timer_Tick method. You should re-write it as follows:

void _timer_Tick(object sender, EventArgs e)
{
    if (!backgroundWorker1.IsBusy)
    {
        backgroundWorker1.RunWorkerAsync();
    }
}
Comments