在刻度中间停止计时器 [英] Stop timer in the middle of the tick

查看:24
本文介绍了在刻度中间停止计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止计时器,但要在滴答的中间,在滴答结束之前.但是当我使用按钮停止计时器时,它只会在滴答完成他的所有功能后才会停止.

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.

我正在使用 4 个单选按钮和一个不断更改所选单选按钮的计时器.当我按下停止按钮时,我的计时器停止,我需要知道选择了哪个单选按钮,但计时器在滴答完成后停止,因此选定的单选按钮发生变化.

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.

代码是:

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

    }
}

推荐答案

我没有按照您在代码中尝试完成的操作进行操作,但在我看来您没有正确使用计时器.

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.

你应该这样做:

在每个计时器上.勾选更改选定的单选框.

On each timer.Tick change the selected radiobox.

你实际上在做什么:

在每个计时器上.勾选更改所有单选框,更改之间有一个时间间隔.

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.

我的建议.将单选按钮添加到 ArrayList.然后在 Timer 刻度上的每个 ArrayList 项之间循环选择.

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

这篇关于在刻度中间停止计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆