DispatcherTimer in WPF

When you want to set a timer working with GUI, you always come across threading problem. In such scenario, .Net indeed makes programmers life easier. It only matters that you choose the right timer to use.

In Win Form, you need to use System.Windows.Forms.Timer.

In WPF, the one is System.Windows.Threading.DispatcherTimer.

Here is a simple sample code for DispatcherTimer.

{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(someInterval);
timer.Tick += new EventHandler(someEventHandler);
timer.Start();
}

{
private void someEventHandler(Object sender, EventArgs args)
{
some operations
//if you want this event handler executed for just once
// DispatcherTimer thisTimer = (DispatcherTimer)sender;
// thisTimer.Stop();
}

P.S.

For general purpose, you can use System.Threading.Timer.

For server-based purpose, System.Timers.Timer can be the right choice.

~ by Martin on September 7, 2007.

14 Responses to “DispatcherTimer in WPF”

  1. hi hi, thks for your content …

  2. Thanks you for sharing your experience. I want to refesh textbox.text every seconds from someEventHandler, but the value does not displayed in texttox.
    Do I need to refresh textbox or set waiting time?
    and how? Thanks you

  3. Thanks a Ton.

  4. Thanks a million.

  5. muchas gracias por el tip

  6. I could not read the article proper;y due to the darkness and color combination of the template I wish I could read that Bu thave no time to give strain while other readable articles are there.

  7. Thanks

  8. Check this:

    http://codex.wordpress.org/Writing_Code_in_Your_Posts

  9. Great article, thanks.

  10. NICE PASTE AND COPY DUMBASS
    I GUESS IF YOU WANT TO READ CRAPOLA
    READ WHAT THIS SOB AUTHOR POSTS
    ROFL

  11. Just what I needed to see. Thanks.

  12. […] https://wangmo.wordpress.com/2007/09/07/dispatchertimer-in-wpf/ […]

  13. L’évent tick n’est pas précis sur cette page

  14. sadasdasd

Leave a comment