C# 定时器不工作';行不通

C# 定时器不工作';行不通,c#,timer,C#,Timer,是我第一次使用定时器,所以可能我做错了什么 我的代码是这样的: private void timer1_Tick(object sender, EventArgs e) { button2.PerformClick(); } private void button2_Click(object sender, EventArgs e) { // random code here timer1.Interv

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

我的代码是这样的:

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

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

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

对不起,如果这是一个很容易的错误,我从这个开始,不知道我做错了什么

谢谢你读我的书!:D

给你

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.
    }
给你

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.
    }
计时器事件(对于每种计时器)不需要您手动调用

您可以设置其事件处理程序方法,设置间隔并启动它。
当需要调用时,底层框架将调用您的
勾选
事件

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

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;
    }
}
计时器事件(对于每种计时器)不需要您手动调用

您可以设置其事件处理程序方法,设置间隔并启动它。
当需要调用时,底层框架将调用您的
勾选
事件

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

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;
    }
}
只将Tick()事件连接一次,最好是通过IDE连接一次,这样就不会有多个处理程序,每个Tick()事件的代码运行不止一次

*在下面的示例中,我将它连接到构造函数中作为替代…但是不要这样做并且通过IDE连接它,否则它会为每个勾号()触发两次

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

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

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

}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
timer1.Enabled=false;
timer1.Interval=(int)TimeSpan.FromSeconds(5).Total毫秒;
timer1.Tick+=timer1\u Tick;//只需连接一次!
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
如果(!timer1.Enabled)
{
Foo();//只将Tick()事件连接一次,最好是通过IDE,这样就不会有多个处理程序,并且每个Tick()事件的代码运行不止一次

*在下面的示例中,我将它连接到构造函数中作为替代…但是不要这样做并且通过IDE连接它,否则它会为每个勾号()触发两次

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

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

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

}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
timer1.Enabled=false;
timer1.Interval=(int)TimeSpan.FromSeconds(5).Total毫秒;
timer1.Tick+=timer1\u Tick;//只需连接一次!
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
如果(!timer1.Enabled)
{

Foo();//嗯..什么不起作用?这段代码似乎有一个无限循环。嗯..什么不起作用?这段代码似乎有一个无限循环。你完全正确。勾号事件应该在Form_Load事件或设计器中断开连接或只关联一次。我已更改了答案以反映你的观察结果。(并向上投票)您完全正确。勾号事件应在Form_Load事件或设计器中断开连接或仅关联一次。我已更改我的答案以反映您的观察。(并向上投票)请同时查看“空闲思维”的答案,因为他提出了一个好的观点。我调整了我的答案以反映其观察结果。请同时查看“空闲思维”的答案,因为他提出了一个好的观点。我调整了我的答案以反映其观察结果。