【问题标题】:Timer doesn't work定时器不工作
【发布时间】:2015-01-16 12:12:26
【问题描述】:

我是第一次使用计时器,所以可能我做错了什么。

我的代码是这样的:

 private void timer1_Tick(object sender, EventArgs e)
    {
        button2.PerformClick();
    }

    private void button2_Click(object sender, EventArgs e)
    {

        // random code here
            timer1.Interval = 5000;
           timer1.Start();
           timer1_Tick(null,null);

    }

我想要做的是:执行随机代码,然后等待计时器间隔并执行 Tick(这将再次在按钮中执行“单击”,再次执行相同的操作),然后重复此操作永远。

对不起,如果是一个简单的错误,我从这个开始,不知道我做错了什么。

感谢您阅读我! :D

【问题讨论】:

标签: c# timer


【解决方案1】:

您无需手动调用 Timer 事件(适用于所有类型的计时器)。

您设置它的事件处理方法,设置间隔并启动它。
底层框架会在需要调用 Tick 事件时调用它。

因此,您需要将 随机代码 放在一个子组件中,您可以从 Tick 事件和按钮单击事件调用该子组件。此外,您应该考虑阻止计时器的进一步激活。您可以禁用该按钮,当您完成随机代码并且条件为真时,停止计时器并重新启用该按钮。

private void button2_Click(object sender, EventArgs e)
{
    stopTheTimer = false;
    YourCommonMethod();
    button2.Enabled = false;
    timer1.Tick += timer1_Tick
    timer1.Interval = 5000;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    YourCommonMethod();
}
private void YourCommonMethod()
{
    // execute your 'random' code here
    if(stopTheTimer)
    {
        timer1.Stop();
        timer1.Tick -= timer1_Tick;  // disconnect the event handler 
        button2.Enabled = true;
    }
}

【讨论】:

  • 请看@Idle_Mind 的回答,因为他的观点很好。我已调整我的答案以反映其观察结果。
【解决方案2】:

给你。 . . .

private void button2_Click(object sender, EventArgs e)
    {
        // random code here
        timer1.Interval = 5000;
        timer1.Start();
        timer1.Tick += timer1_Tick;

    }

    void timer1_Tick(object sender, EventArgs e)
    {
        //Your timing code here.
    }

【讨论】:

    【解决方案3】:

    只连接 Tick() 事件一次,最好通过 IDE,这样您就不会遇到多个处理程序,并且您的代码不会为每个 Tick() 事件运行多次。 p>

    *在下面的示例中,我已在构造函数中将其连接起来作为替代方案......但不要这样做并且通过 IDE 连接它,否则它将为每个触发两次滴答()。

    您可能只想在 Button 处理程序中调用 Start(),然后在 Tick() 事件中调用您的“随机代码”,如下所示:

    public partial class Form1 : Form
    {
    
        public Form1()
        {
            InitializeComponent();
            timer1.Enabled = false;
            timer1.Interval = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
            timer1.Tick += timer1_Tick; // just wire it up once!
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                Foo(); // <-- Optional if you want Foo() to run immediately without waiting for the first Tick() event
                timer1.Start();
            }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            Foo();
        }
    
        private void Foo()
        {
            Console.WriteLine("Random Code @ " + DateTime.Now.ToString());
        }
    
    }
    

    这与其他答案没有太大不同,但多次连接 Tick() 事件是一个严重的缺陷......

    【讨论】:

    • 你是绝对正确的。 Tick 事件应在 Form_Load 事件或设计器中断开或仅关联一次。我已更改答案以反映您的观察。 (并赞成你的)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2011-10-18
    • 2013-03-27
    • 2023-03-18
    • 2016-01-09
    • 2016-01-08
    相关资源
    最近更新 更多