C# 定时器启动时调用 Tick 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12073703/
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 20:16:18  来源:igfitidea点击:

call Tick event when timer starts

c#timer

提问by topofsteel

I'm using 'System.Windows.Forms.Timer' to repeat a task. But when the timer starts, I have to wait for one interval before the task starts. The interval is set to 10 seconds to give the task enough time to do it's thing. But there is an 'awkward silence' waiting for it to start the first time. Is there a way to trigger the Tick event when the timer is enabled? (I am unable to use threading, callbacks or events to get the task repeat)

我正在使用“System.Windows.Forms.Timer”来重复任务。但是当计时器启动时,我必须等待一个时间间隔才能开始任务。间隔设置为 10 秒,以便为任务提供足够的时间来完成它的工作。但是有一种“尴尬的沉默”等待它第一次开始。有没有办法在定时器启用时触发 Tick 事件?(我无法使用线程、回调或事件来使任务重复)

    private int counter;
    Timer t = new Timer();

    private void InitializeTimer()
    {
        counter = 0;
        t.Interval = 750;
        t.Enabled = true;

        t.Tick += new EventHandler(timer1_Tick);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (counter >= 3)
        {
            t.Enabled = false;                
        }
        else
        {
            //do something here
            counter++;
        }
    }

采纳答案by JleruOHeP

You can always call your method manually:

您始终可以手动调用您的方法:

private void InitializeTimer()
{
    counter = 0;
    t.Interval = 750;
    t.Enabled = true;
    timer1_Tick(null, null);

    t.Tick += new EventHandler(timer1_Tick);
}

回答by Rawling

The simplest way might be to initially set the Intervalto something very small, then increase it in the Tickhandler, or in a separate handler.

最简单的方法可能是最初将 设置为Interval很小的值,然后在Tick处理程序中或在单独的处理程序中增加它。

This will ensure the first "tick" occurs in the same manner as subsequent ticks, e.g. on its own thread, rather than in the context of the initialize method.

这将确保第一个“滴答”以与后续滴答相同的方式发生,例如在它自己的线程上,而不是在 initialize 方法的上下文中。

回答by Richard Friend

Just create a method then call this from within your timer and also just before you start your timer.

只需创建一个方法,然后在您的计时器中以及在您启动计时器之前调用它。

private int counter; 
Timer t = new Timer(); 

private void InitializeTimer() 
{ 
    counter = 0; 
    t.Interval = 750; 

    DoMything();
    t.Tick += new EventHandler(timer1_Tick); 
    t.Enabled = true; 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    if (counter >= 3) 
    { 
        t.Enabled = false;                 
    } 
    else 
    { 
        //do something here 
        counter++; 
        DoMything();
    } 
} 


private void DoMything()
{
   //Do you stuff here
}

回答by ChrisF

You could use a System.Threading.Timer.

你可以使用一个System.Threading.Timer.

This has a constructorthat takes an initial wait period. Set this to zero and the timer will trigger the callback immediately then every interval you specify thereafter.

它有一个需要初始等待期的构造函数。将此设置为零,计时器将立即触发回调,然后是您指定的每个间隔。

Timer stateTimer = new Timer(tcb, autoEvent, 0, 750);