views:

1026

answers:

1

Hi

When using System.Threading.Timer, and initializing a windows form on the timer's tick event, the form becomes unresponsive. Why is that, and how can I avoid it?

This simple sample code shows the problem; the two first windows ("Original" and "Manual") works fine, but "Timer" becomes unresponsive at once.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1.Forms
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = "Original";
            this.Left = 0;

            Form f = new Form();
            f.Text = "Manual";
            f.Show();
            f.Left = this.Width;

            TimerCallback tCallback = new TimerCallback(Timer_Tick);
            System.Threading.Timer timer = new System.Threading.Timer(tCallback, null, 1000, System.Threading.Timeout.Infinite);
        }

        void Timer_Tick(object o)
        {
            Form f = new Form();
            f.Text = "Timer";
            f.Show();
            f.Left = this.Width * 2;
        }
    }
}
+4  A: 

You're using a System.Threading.Timer, which will execute on a thread pool thread - i.e. one which doesn't have an event loop associated with it.

If you use System.Windows.Forms.Timer, it will execute on the UI thread, and all will be well. Alternatively, use a System.Timers.Timer and set the SynchronizingObject to the parent form.

Jon Skeet
No need, I changed it to the system.windows.forms timer. I was just using the system.threading.timer because I just used that on another project, and didn't really think this time. Thanks!
Erlend D.
Righto - saves me a job :)
Jon Skeet