调用Tick事件计时器启动时 [英] call Tick event when timer starts

查看:144
本文介绍了调用Tick事件计时器启动时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用'System.Windows.Forms.Timer重复任务。但计时器开始的时候,我必须等待一个间隔任务开始之前。时间间隔设置为10秒,得到任务足够的时间做的事情。但有一个尴尬的沉默等待它启动的第一次。有没有启用定时器时触发Tick事件的方法吗? (我不能使用线程,回调或事件来获得任务重复)

 私人诠释柜台; 
定时T =新的Timer();

私人无效InitializeTimer()
{
计数器= 0;
t.Interval = 750;
t.Enabled = TRUE;

t.Tick + =新的EventHandler(timer1_Tick);将
}

私人无效timer1_Tick(对象发件人,EventArgs五)
{
如果(计数器> = 3)
{$ B $(B T) .Enabled = FALSE;
}
,否则
{
//做的东西在这里
计数器++;
}
}


解决方案

您可以随时拨打手动你的方法:

 私人无效InitializeTimer()
{
计数器= 0;
t.Interval = 750;
t.Enabled = TRUE;
timer1_Tick(NULL,NULL);

t.Tick + =新的EventHandler(timer1_Tick);将
}


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)

    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++;
        }
    }

解决方案

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);
}

这篇关于调用Tick事件计时器启动时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆