开发者

Firing method on interval with System.Threading.Timer in winForm C#

What I want to do is to use the System.Threading.Timer to execute a method with a interval. My example code looks like this atm.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        System.Threadi开发者_开发百科ng.Timer t1 = new System.Threading.Timer(WriteSomething, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(10));

    }
    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Clear();
    }

    public void WriteSomething(object o)
    {

        textBox1.Text = "Test";
    }
}

}

Isn't this suppost to execute the WriteSomething method every 10'th second. What rly happens is that the WriteSomething is executed when I run my application and after 10 seconds the application closes. Think I have missunderstood how this works, can anyone tell me how to do this with the System.Threading.Timer.

thanks in advance, code examples are very welcome


The more likely scenario is that it crashes after 10 seconds. You cannot touch any controls in the callback, it runs on the wrong thread. You'd have to use Control.BeginInvoke(). Which makes it utterly pointless to use a System.Threading.Timer instead of a System.Windows.Forms.Timer.

Be practical. Make it 100 milliseconds so you don't grow a beard waiting for the crash. And don't use an asynchronous timer to update the UI, it is useless.


FYI, there is nothing about System.Windows.Forms timer that doesn't allow you to create in code (it's not just a "drag-and-drop" timer). Code:

Constructor code:

  System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
  timer.Tick += OnTimerTick;
  timer.Interval = 10000;
  timer.Start();

Event Handler:

  private void OnTimerTick(object sender, EventArgs e)
  {
    // Modify GUI here.
  }


Just to reiterate what Hans said, in a WinForms application all GUI elements are not inherently thread-safe. Almost all methods / properties on Control classes can only be called on the thread the GUI was created on. The System.Threading.Timer invokes its callback on a thread pool thread, not the the thread you created the timer on (see reference below from MSDN). As Hans said, you probably want a System.Windows.Forms.Timer instead, that will invoke your callback on the correct thread.

You can always verify whether you can call methods on a Control (assuring you're on the correct thread) by using the code:

System.Diagnostics.Debug.Assert(!InvokeRequired);

inside your event handler. If the assert trips, you're on a thread that cannot modify this Control.

Quote from MSDN help on System.Threading.Timer on the callback method you passed in the constructor:

The method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system.


Common error: need to keep timer variable as class member as garbage collector may kill it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜