Nicholas Duboc
Nicholas Duboc

Reputation: 21

Stop timer in the middle of the tick

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

    }
}

Upvotes: 2

Views: 2704

Answers (4)

Paciente8159
Paciente8159

Reputation: 181

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.

Upvotes: 1

JleruOHeP
JleruOHeP

Reputation: 10386

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.

Upvotes: 0

Menelaos Vergis
Menelaos Vergis

Reputation: 3955

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

Upvotes: 0

alex
alex

Reputation: 12654

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.

Upvotes: 1

Related Questions