Export (0) Print
Expand All
5 out of 14 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:  WindowsBase (in WindowsBase.dll)
XMLNS for XAML: Not mapped to an xmlns.
public class DispatcherTimer

The DispatcherTimer type exposes the following members.

  NameDescription
Public methodDispatcherTimer()Initializes a new instance of the DispatcherTimer class.
Public methodDispatcherTimer(DispatcherPriority)Initializes a new instance of the DispatcherTimer class which processes timer events at the specified priority.
Public methodDispatcherTimer(DispatcherPriority, Dispatcher)Initializes a new instance of the DispatcherTimer class which runs on the specified Dispatcher at the specified priority.
Public methodDispatcherTimer(TimeSpan, DispatcherPriority, EventHandler, Dispatcher)Initializes a new instance of the DispatcherTimer class which uses the specified time interval, priority, event handler, and Dispatcher.
Top
  NameDescription
Public propertyDispatcherGets the Dispatcher associated with this DispatcherTimer.
Public propertyIntervalGets or sets the period of time between timer ticks.
Public propertyIsEnabledGets or sets a value that indicates whether the timer is running.
Public propertyTagGets or sets a user-defined data object.
Top
  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStartStarts the DispatcherTimer.
Public methodStopStops the DispatcherTimer.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public eventTickOccurs when the timer interval has elapsed.
Top

The DispatcherTimer is reevaluated at the top of every Dispatcher 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 Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.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 System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.

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

The following example creates a DispatcherTimer that updates the contents of a Label and calls the InvalidateRequerySuggested method on the CommandManager.

A DispatcherTimer object named dispatcherTimer is created. The event handler dispatcherTimer_Tick is added to the Tick event of dispatcherTimer. The Interval is set to 1 second using a TimeSpan object, and the timer is started.

//  DispatcherTimer setup
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();

The Tick event handler updates a Label that displays the current second, and it calls InvalidateRequerySuggested on the CommandManager.

//  System.Windows.Threading.DispatcherTimer.Tick handler 
// 
//  Updates the current seconds display and calls 
//  InvalidateRequerySuggested on the CommandManager to force  
//  the Command to raise the CanExecuteChanged event. 
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    // Updating the Label which displays the current second
    lblSeconds.Content = DateTime.Now.Second;

    // Forcing the CommandManager to raise the RequerySuggested event
    CommandManager.InvalidateRequerySuggested();
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft. All rights reserved.