C# 相当于 30 分钟的计时器滴答声是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10011230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:07:17  来源:igfitidea点击:

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

c#timer

提问by geoNeo_

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?

我有一个计时器滴答声,我想每 30 分钟启动一次我的后台工作人员。计时器滴答的 30 分钟的等效值是多少?

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.

我不确定这是否是最好的方法,或者是否有人有更好的建议。

采纳答案by RB.

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

计时器的Interval属性以毫秒为单位指定,而不是滴答。

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

因此,对于每 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;

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

但是,我不清楚Tick您使用的事件是什么。我想你的意思是Elapsed

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

编辑正如 CodeNaked 所说,你在谈论System.Windows.Forms.Timer,而不是System.Timers.Timer。幸运的是,我的回答适用于两者:)

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

最后,我不明白你为什么_ticks在你的timer_Tick方法中维护一个 count ( ) 。您应该按如下方式重新编写它:

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

回答by bitsugar

Didn't get the question well. But if u just want the interval of 30 minutes then give timer1.interval = 1800000;

没有很好地回答这个问题。但是如果你只想要 30 分钟的间隔,那么给 timer1.interval = 1800000;

// There are 10,000 ticks in a millisecond (Dont forget this )

// 一毫秒有 10,000 个滴答声(不要忘记这一点)

回答by weston

To make the code more readable, you can use the TimeSpanclass:

为了使代码更具可读性,您可以使用TimeSpan该类:

_timer.Interval = TimeSpan.FromMinutes(30).TotalMilliseconds;

回答by Apple Lei

using Timer = System.Timers.Timer;

[STAThread]

static void Main(string[] args) {
    Timer t = new Timer(1800000); // 1 sec = 1000, 30 mins = 1800000 
    t.AutoReset = true;
    t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
    t.Start(); 
}

private static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
// do stuff every 30  minute
}