0 out of 3 rated this helpful - Rate this topic

DispatcherTimer Class

A timer that is integrated into the Dispatcher queue, which is processed at a specified interval of time and at a specified priority.

System.Object
  System.Windows.Threading.DispatcherTimer

Namespace:  System.Windows.Threading
Assembly:  System.Windows (in System.Windows.dll)
public class DispatcherTimer

The DispatcherTimer type exposes the following members.

  Name Description
Public method Supported by Silverlight for Windows Phone DispatcherTimer Initializes a new instance of the DispatcherTimer class.
Top
  Name Description
Public property Supported by Silverlight for Windows Phone Interval Gets or sets the amount of time between timer ticks.
Public property Supported by Silverlight for Windows Phone IsEnabled Gets a value that indicates whether the timer is running.
Top
  Name Description
Public method Supported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by Silverlight for Windows Phone Start Starts the DispatcherTimer.
Public method Supported by Silverlight for Windows Phone Stop Stops the DispatcherTimer.
Public method Supported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public event Supported by Silverlight for Windows Phone Tick Occurs when the timer interval has elapsed.
Top

The DispatcherTimer is reevaluated at the top of every DispatcherTimer loop.

Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the DispatcherTimer queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

If a System.Threading.Timer is used, 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 UI thread, it is necessary to post the operation onto the UI thread using Dispatcher.BeginInvoke. This is unnecessary when using a DispatcherTimer.

A DispatcherTimer will keep an object alive whenever the object's methods are bound to the timer.

The following code example demonstrates how to use this class.


private void TestDispatcherTimer(Panel counterPanel)
{
    DispatcherTimer timer = new DispatcherTimer();
    int counter = 0;

    counterPanel.MouseLeftButtonDown += 
        delegate(object s, MouseButtonEventArgs args) {
            if (timer.IsEnabled) timer.Stop(); else timer.Start(); 
        };

    timer.Tick += 
        delegate(object s, EventArgs args) {
            counterPanel.Children.Clear();
            counterPanel.Children.Add( new TextBlock { 
                Text = counter++.ToString() });
        };

    timer.Interval = new TimeSpan(0, 0, 1); // one second
    timer.Start();
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Finalize?
This page shows Finalize as a method.    It does not appear as a choice using Windows Phone 7.1.  All the other methods are there.
Dispatcher Timer page content mistake.
If a System.Threading.Timer is used, 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 UI thread, it is necessary to post the operation onto the DispatcherTimer(must be Timer) of the UI thread using Dispatcher.BeginInvoke. This is unnecessary when using a DispatcherTimer.$0