简体   繁体   中英

“drag and drop to form” timer in visual studio express 2013 doesn't work

I have no idea why this code does not work.

The timer just seams to freeze everything. As far as I know, the timer is sent to a newly created thread. I've tried locking on tick, but no luck.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public int CountUp = 0;
        public Form1()
        {
            InitializeComponent();
            label1.Text = "Click To Start Timer";
            timer1.Enabled = false;
            timer1.Stop();
            timer1.Interval = 1000;

        }

        private void button1_Click(object sender, EventArgs e)
        {

            bool Toggled = false;
            if (!Toggled)
            {
                timer1.Enabled = true;
                Toggled = true;
            }
            else
            {
                timer1.Enabled = false;
                Toggled = false;
                label1.Text = "Timer Stopped";
                CountUp = 0;
            }
            while(Toggled){
                label1.Text = "Timer1 Running";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //lock (label2.Text) <- froze the program immediately
            //{
            //    CountUp++;
            //    string CurrentTime = CountUp.ToString();
            //    label2.Text = CurrentTime;
            //}
            CountUp++;
            string CurrentTime = CountUp.ToString();
            label2.Text = CurrentTime;
            //CurrentTime = timer1.ToString() = Garbage & freeze.
        }

    }
}

Any suggestions? It may be the key to solving this question .

The while(Toggled){ .. } code will freeze the interface and hang the program as it blocks the UI thread. However, the Forms.Timer , which relies on the UI event queue being processed, will never fire due to the blocking loop - as such, the flag variable will never be "reset".

While one of the threaded timers could be used, it would still result in an un-responsive UI. The solution is to design [WinForm] programs to react to events , such as when the timer starts and when it stops 1 .


1 There are actually no start/stop events per-se, but do the actions when the timer is started or stopped by the code. For example,

private void button1_Click(object sender, EventArgs e)
{
    if (!timer1.Enabled) {
        CountUp = 0;
        timer1.Enabled = true;
        label1.Text = "Timer1 Running";
    } else {
        timer1.Enabled = false;
        label1.Text = "Timer1 Stopped (Manual)";
    }
}

// Later on, when a tick occurs and the timer should end..
// The timer callback runs on the UI thread it was created on.
private void timer1_Tick(object sender, EventArgs e)
{
    CountUp++;
    if (CountUp > 10) {
        timer1.Enabled = false;
        label1.Text = "Timer1 Stopped (Time up)";
    }
}

There is no need to use lock at all because all of the code should be running on the UI thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM