Timer vs. DispatcherTimer in WPF
Published on August 6th, 2008.
Have you ever wondered what is the difference between Timer and DispatcherTimer in WPF / Silverlight? In this post I will try to explain you the little difference. Timer generates recurring events in an application. DispatcherTimer is a timer that is integrated into the Dispatcher queue which is processed at a specified interval of time and at a specified priority. Timers are not guaranteed to execute exactly when the time interval occurs, but are guaranteed not to execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes, it is dependent of the other jobs in the queue and their priorities.
If a Timer is used in a WPF application, it is worth noting that the Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer opposed to a Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set.
So, the right way to do schedule operations in WPF / Silverlight application is the following:
DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(100); timer.Tick += new EventHandler(timer_Tick); timer.Start(); |
Filled under Silverlight, WPF.
7 Comments
Johan Hernandez on May 4th, 2009
Very helpful.
Thanks.
Ryan on October 12th, 2009
Do you have any input on dealing with DispaterTimer overload. I am using it to run multiple objects and after about 5 hours or so the CPU reaches 80% and it no longer functions as expected. I’m afraid that they are NOT being reclaimed with the GC.
Thanks
Boyan Mihailov on October 12th, 2009
Sorry Ryan, but I have no input on this. What kind of object are you loading? Maybe you should destroy them when they are no longer used in order to free resources.
Grayson Peddie on May 1st, 2010
I should note that you need to add System.Windows.Threading to the list of “using” imports above your code.
A Real Developer on November 27th, 2011
So, copying and pasting the remarks from the MSDN library counts as “explaining” now? Lame. Very lame.

Dùng CompositionTarget.Rendering trong SL2 để tạo Animation là tối ưu… - Coding, training dog, playing Aikido, harmonica and.... on March 16th, 2009
[...] (Xem thêm so sánh sự khác biệt giữa Timer và DispatcherTimer ở trang timer-vs-dispatchertimer-in-wpf/) [...]