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.
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();
}
}
To make the code more readable, you can use the TimeSpan
class:
_timer.Interval = TimeSpan.FromMinutes(30).TotalMilliseconds;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With