Using timer in windows form application

Jar*_*rad 1 c# timer winforms

I am new to coding and are having trouble.

I want to use a timer that will start upon pressing ScatterMode tab, it will start counting 4sec before running "Dosomething" function. This cycle will repeat itself until i decide to stop the program. But the problem i get is, this code only run correctly for like 2loop, after that the timer sort of go crazy LOL.

     System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

    //ScatterMode Tab
    private void Scatter_modeToolStripMenuItem_Click(object sender, EventArgs e)
    {
            timer.Interval = 4000;
            timer.Enabled = true;
            timer.Tick += new EventHandler(Dosomething);
            timer.Start();
    }

    private void Dosomething (object sender, EventArgs e)
    {
        timer.Stop();
        timer.Enabled = false;

        Grab.buffer(out buffer, out status, 6000);
        Scatter_mode(buffer);
        pictureBox1.Refresh();

        int done_grab = 1;

        if (doneGrab == 1)
        {
            timer.Interval = 4000;
            timer.Enabled = true;
            timer.Tick += new EventHandler(Scatter_modeToolStripMenuItem_Click);
            timer.Start();
            done_grab = 0;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Cai*_*ard 5

向计时器添加新的事件处理程序以处理其滴答事件,在滴答事件的处理程序内部确实会导致计时器变得“疯狂”。每次计时器引发其事件时,都会添加另一个事件处理程序(响应引发的事件)。这意味着下次计时器计时时,事件代码将运行两次。将添加两个新的事件处理程序。下次计时器计时时,代码将运行 4 次。将添加 4 个事件处理程序...等等

从代码中删除这一行:

timer.Tick += new EventHandler(Scatter_modeToolStripMenuItem_Click);
Run Code Online (Sandbox Code Playgroud)

并将此行移至表单的构造函数中:

timer.Tick += new EventHandler(Dosomething);
Run Code Online (Sandbox Code Playgroud)

您只想连接此事件处理程序一次。每次计时器的时间间隔过去,代码就会运行一次:)

我还将对您的代码进行一些同行评审,请参阅评论:

 System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

//ScatterMode Tab
private void Scatter_modeToolStripMenuItem_Click(object sender, EventArgs e)
{
        timer.Interval = 4000; //can go in the constructor also; don't need to set repeatedly
        timer.Enabled = true;
        timer.Tick += new EventHandler(Dosomething); //move to constructor
        timer.Start(); //this isn't needed - you already Enabled the timer, which started it
}

private void Dosomething (object sender, EventArgs e)
{
    timer.Stop();          //use this
    timer.Enabled = false; //or this. It's not required to do both

    Grab.buffer(out buffer, out status, 6000);  //if these lines crash then your timer will
    Scatter_mode(buffer);                       //only restart if the toolstripmenuitemclick
    pictureBox1.Refresh();                      //above runs.. is it what you wanted?

    int done_grab = 1; //not needed

    if (doneGrab == 1) //this will always evaluate to true, it is not needed
    {
        timer.Interval = 4000; //the interval is already 4000, not needed
        timer.Enabled = true; //careful; your timer may stop forever if the code above crashes
        timer.Tick += new EventHandler(Scatter_modeToolStripMenuItem_Click); //remove
        timer.Start(); //not needed
        done_grab = 0; //not needed
    }
}
Run Code Online (Sandbox Code Playgroud)