Stop timer in the middle of the tick

2019-07-13 02:37发布

问题:

I want to stop a timer but in the middle of the tick, before the tick finishes. But when I use a button to stop the timer, it only stops it after the tick finishes all his functions.

I'm using 4 radiobuttons and a timer that keeps changing the selected radiobutton. When I press the Stop button, my timer stops and I need to know what radiobutton is selected, but the timer just stops after the tick finishes, so the selected radiobutton changes.

The code is:

namespace teste_bola
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Int32 x = 1000;

        private void ini_Click(object sender, EventArgs e)
        {

            timer1.Enabled = true;
        }

        private void par_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
        private void t_Click(object sender, EventArgs e)
        {
            Int32 Intervalo = Convert.ToInt32(tb.Text);
            x = Intervalo;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            r3.Select();
            System.Threading.Thread.Sleep(x);
            r2.Select();
            System.Threading.Thread.Sleep(x);
            r1.Select();
            System.Threading.Thread.Sleep(x);
            r2.Select();
            System.Threading.Thread.Sleep(x);
            r3.Select();
            System.Threading.Thread.Sleep(x);
            r4.Select();
            System.Threading.Thread.Sleep(x);
        }

    }
}

回答1:

You can introduce a flag bool shouldStop , set it when button is clicked, and check it from the Tick handler. If it is true, then just exit the method.



回答2:

I'm not following what you are trying to accomplish in you code but it seems to me like you are not using the timer correctly.

You should do something like:

On each timer.Tick change the selected radiobox.

What you are actually doing is:

On each timer.Tick change through all radiobox's with a time interval between changes.

This means that on the first timer timeout the tick code is executed. On the second timer timeout the first tick job was not yet finnished.

This will lead to erratic behaviour to say the least.

My advice. Add the radio buttons to a ArrayList. Then cicle select between each of the ArrayList items on a Timer tick.



回答3:

try the following

private void timer1_Tick(object sender, EventArgs e)
    {
        r3.Select();
        System.Threading.Thread.Sleep(x);
        if(timer1.Enabled == false) return;
            r2.Select();
            System.Threading.Thread.Sleep(x);
            if(timer1.Enabled == false) return;
            r1.Select();
            System.Threading.Thread.Sleep(x);
            if(timer1.Enabled == false) return;
            r2.Select();
            System.Threading.Thread.Sleep(x);
            if(timer1.Enabled == false) return;
            r3.Select();
            System.Threading.Thread.Sleep(x);
            if(timer1.Enabled == false) return;
            r4.Select();
            System.Threading.Thread.Sleep(x);
            }


回答4:

You can face this behaviour because your thread is sleeping during the execution of the timer1_Tick and thus there might be a queye of events.

As alex proposed you can add a flag, but you should check it after every sleep or, even better, put your animation in the different thread.



标签: c# timer