我有一个计时器打勾,我想每隔30分钟开始我的背景工作.计时器滴答的等效值为30分钟是多少?
下面是代码:
_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;
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定这是最好的方式,还是有人有更好的建议.
RB.*_*RB. 13
计时器的Interval属性以毫秒为单位,而不是刻度.
因此,对于每30分钟触发一次的计时器,只需执行以下操作:
// 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;
Run Code Online (Sandbox Code Playgroud)
但是,我不清楚Tick你使用的是什么.我觉得你的意思是Elapsed?
编辑正如CodeNaked所说,你在谈论System.Windows.Forms.Timer,而不是System.Timers.Timer.幸运的是,我的回答适用于两个:)
最后,我不明白你为什么_ticks在你的timer_Tick方法中维护count().您应该按如下方式重写它:
void _timer_Tick(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
为了使代码更具可读性,您可以使用该类TimeSpan:
_timer.Interval = TimeSpan.FromMinutes(30).TotalMilliseconds;
Run Code Online (Sandbox Code Playgroud)