A timer that is integrated into the Dispatcher queue, which is processed at a specified interval of time and at a specified priority.
Namespace:
System.Windows.Threading
Assembly:
System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public Class DispatcherTimer
Dim instance As DispatcherTimer
public class DispatcherTimer
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 DispatcherTimer of 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 WithEvents counterPanel As Panel
Private WithEvents timer As DispatcherTimer = New DispatcherTimer()
Private counter As Integer = 0
Private Sub PanelClick(ByVal sender As System.Object, _
ByVal e As System.Windows.Input.MouseButtonEventArgs) _
Handles counterPanel.MouseLeftButtonDown
If timer.IsEnabled Then timer.Stop() Else timer.Start()
End Sub
Private Sub TimerClick(ByVal sender As System.Object, _
ByVal e As EventArgs) Handles timer.Tick
counter += 1
counterPanel.Children.Clear()
Dim t As New TextBlock
t.Text = counter.ToString
counterPanel.Children.Add(t)
End Sub
Private Sub TestDispatcherTimer(ByVal p As Panel)
Me.counterPanel = p
timer.Interval = New TimeSpan(0, 0, 1)
timer.Start()
End Sub
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();
}
System..::.Object
System.Windows.Threading..::.DispatcherTimer
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference