none
Timer Control in WPF

    Question

  •  

    Hi all, I tried to use the timer control in WPF but it does not to work. If i put a Messagebox.show command in the tick method of the timer control, it loops endlessly.However, when i used the same code in a normal windows form, it works. If you could look at my code below and give me some suggestions, it would be great. Thanks

     

     

    public partial class Window2 : Window{

     

    private System.Timers.Timer timer = new System.Timers.Timer(1000);

     

    public Window2(){

    InitializeComponent();

    }

     

     

    private void Window_Loaded(object sender, RoutedEventArgs e){

    this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);

    timer.Enabled = true;

    timer.Start();

    }

     

    private void timer1_Tick(object sender, EventArgs e){

    this.Close();

    }

     

     

    }

     

    Monday, December 10, 2007 1:10 AM

Answers

  • I don't think the System.Timers.Timer is designed to work with WPF user interfaces. This timer raises the Elapsed event on a background worker thread, and when the event handler attempts to call Close() an exception is thrown (the exception is handled "killed" by the Timer object).

     

    Instead you should use a DispatcherTimer like this:

     

    Code Block

    //private Timer timer = new System.Timers.Timer(1000);

    private DispatcherTimer timer;

     

    public Window1()

    {

        InitializeComponent();

        Loaded += new RoutedEventHandler(Window1_Loaded);

    }

     

    void Window1_Loaded(object sender, RoutedEventArgs e)

    {

        //timer.Elapsed += timer1_Tick

        //timer.AutoReset = false;

        timer = new DispatcherTimer();

        timer.Interval = TimeSpan.FromSeconds(1);

        timer.Tick += timer1_Tick;

       

        timer.Start();

    }

     

    private void timer1_Tick(object sender, EventArgs e)

    {

        timer.Stop();

        this.Close();

    }

     

     

    Hope this helps,

    Tor.

     

    ----------

    Tor Langlo

    Koda Software

    Web: http://www.kodasoftware.com

    Blog: http://torlanglo.spaces.live.com

    Monday, December 10, 2007 3:39 AM

All replies

  • I don't think the System.Timers.Timer is designed to work with WPF user interfaces. This timer raises the Elapsed event on a background worker thread, and when the event handler attempts to call Close() an exception is thrown (the exception is handled "killed" by the Timer object).

     

    Instead you should use a DispatcherTimer like this:

     

    Code Block

    //private Timer timer = new System.Timers.Timer(1000);

    private DispatcherTimer timer;

     

    public Window1()

    {

        InitializeComponent();

        Loaded += new RoutedEventHandler(Window1_Loaded);

    }

     

    void Window1_Loaded(object sender, RoutedEventArgs e)

    {

        //timer.Elapsed += timer1_Tick

        //timer.AutoReset = false;

        timer = new DispatcherTimer();

        timer.Interval = TimeSpan.FromSeconds(1);

        timer.Tick += timer1_Tick;

       

        timer.Start();

    }

     

    private void timer1_Tick(object sender, EventArgs e)

    {

        timer.Stop();

        this.Close();

    }

     

     

    Hope this helps,

    Tor.

     

    ----------

    Tor Langlo

    Koda Software

    Web: http://www.kodasoftware.com

    Blog: http://torlanglo.spaces.live.com

    Monday, December 10, 2007 3:39 AM
  • Hi Tor,

              Your Solution worked great.

     

    Thanks a million!

     

     

    Regards,

    Vijay

     

    Monday, December 10, 2007 3:53 AM
  • Hi Tor,

     

    Thanks. Good job.

     

    Ragards

    Nuri.

    Monday, April 12, 2010 11:09 AM
  • Saturday, October 23, 2010 9:39 PM
  • try this link it may help you

    http://infynet.wordpress.com/2010/10/23/timer-in-wpf/

     

    It's the same tha Tor write...

     

    Thanks Tor

    Wednesday, January 19, 2011 5:18 PM
  • I forgot to mention that you have to add the library:
    using System.Windows.Threading;

     


    Saturday, December 03, 2011 1:54 AM
  • Cool your solution (: Thank you mucho !! XD Mexican english xD

     

    Saturday, December 03, 2011 1:56 AM
  • its work great I didnt know System.timer not work in wpf till I read your article I wonder why the System timer not work. now ı use dispatcher timer  thx  Tor Langlo

    <(+)>--C#_WPF_SİLVERLİGHT_XBAP_HTML5--WCF_<(+)>

    Sunday, September 29, 2013 1:05 PM