DispatcherTimer DispatcherTimer DispatcherTimer Class

Definition

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

public : class DispatcherTimer : IDispatcherTimer
public class DispatcherTimer : IDispatcherTimer
Public Class DispatcherTimer Implements IDispatcherTimer
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

This example code implements a simple console-style timer that writes data to a TextBlock named TimerLog (XAML that defines TimerLog is not shown). The Interval value is set to 1, and the log displays the actual elapsed time for each Tick.

DispatcherTimer dispatcherTimer;
DateTimeOffset startTime;
DateTimeOffset lastTime;
DateTimeOffset stopTime;
int timesTicked = 1;
int timesToTick = 10;

public void DispatcherTimerSetup()
{
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += dispatcherTimer_Tick;
    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    //IsEnabled defaults to false
    TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
    startTime = DateTimeOffset.Now;
    lastTime = startTime;
    TimerLog.Text += "Calling dispatcherTimer.Start()\n";
    dispatcherTimer.Start();
    //IsEnabled should now be true after calling start
    TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
}

void dispatcherTimer_Tick(object sender, object e)
{
    DateTimeOffset time = DateTimeOffset.Now;
    TimeSpan span = time - lastTime;
    lastTime = time;
    //Time since last tick should be very very close to Interval
    TimerLog.Text += timesTicked + "\t time since last tick: " + span.ToString() + "\n";
    timesTicked++;
    if (timesTicked > timesToTick)
    {
        stopTime = time;
        TimerLog.Text += "Calling dispatcherTimer.Stop()\n";
        dispatcherTimer.Stop();
        //IsEnabled should now be false after calling stop
        TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
        span = stopTime - startTime;
        TimerLog.Text += "Total Time Start-Stop: " + span.ToString() + "\n";
    }
}
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
    DispatcherTimerSetup();
}

// .cpp definition, .h not shown
void MainPage::StartTimerAndRegisterHandler() {
    auto timer = ref new Windows::UI::Xaml::DispatcherTimer();
    TimeSpan ts;
    ts.Duration = 500;
    timer->Interval = ts;
    timer->Start();
    auto registrationtoken = timer->Tick += ref new EventHandler<Object^>(this,&MainPage::OnTick);
}
void MainPage::OnTick(Object^ sender, Object^ e) {
    // do something on each tick here ...
}

Remarks

The DispatcherTimer can be used to run code on the same thread that produces the UI thread. Code running on this thread has the privilege to create and modify objects that can only be created and modified on the UI thread. To specify that code should run on the UI thread, set the Interval property and then call the Start method. The Tick event fires after the time specified in Interval has elapsed. Tick continues firing at the same Interval until the Stop method is called, the app terminates, or the app is suspended (fires Suspending ).

One scenario for DispatcherTimer is to check properties on sensors where changes to the sensor values are not purely event-driven, or the events don't give you the granularity you want. You can see this in the Accelerometer sample.

Other scenarios for DispatcherTimer include checking for state changes that don't have related events, or for periodic UI updates that can't use a storyboarded animation or a two-way binding.

Tip

If you're migrating Microsoft Silverlight or Windows Presentation Foundation (WPF) code, the DispatcherTimer and the related Dispatcher was in a separate System.Windows.Threading namespace. There is no Windows.UI.Xaml.Threading namespace in the Windows Runtime, so this class is in Windows.UI.Xaml.

If you aren't doing anything with the UI thread in your Tick handlers and just need a timer, you could also use ThreadPoolTimer instead. Also, for techniques like ThreadPoolTimer or a .NET Task, you aren't totally isolated from the UI thread. You could still assign to the UI thread asynchronously using CoreDispatcher.RunAsync.

Constructors

DispatcherTimer() DispatcherTimer() DispatcherTimer()

Initializes a new instance of the DispatcherTimer class.

public : DispatcherTimer()
public DispatcherTimer()
Public Sub New()

Properties

Interval Interval Interval

Gets or sets the amount of time between timer ticks.

public : TimeSpan Interval { get; set; }
public TimeSpan Interval { get; set; }
Public ReadWrite Property Interval As TimeSpan
Value
TimeSpan TimeSpan TimeSpan

The amount of time between ticks. The default is a TimeSpan

with value evaluated as 00:00:00.

IsEnabled IsEnabled IsEnabled

Gets a value that indicates whether the timer is running.

public : Platform::Boolean IsEnabled { get; }
public bool IsEnabled { get; }
Public ReadOnly Property IsEnabled As bool
Value
Platform::Boolean bool bool

true if the timer is enabled and running; otherwise, false.

Methods

Start() Start() Start()

Starts the DispatcherTimer.

public : void Start()
public void Start()
Public Function Start() As void

Remarks

If the timer has already started, then it is restarted.

Stop() Stop() Stop()

Stops the DispatcherTimer.

public : void Stop()
public void Stop()
Public Function Stop() As void

Remarks

If Stop is called when the timer interval has just elapsed, it's possible that the timer already queued a Tick event. This event will still be raised.

Events

Tick Tick Tick

Occurs when the timer interval has elapsed.

public : event EventHandler Tick<Platform::Object>
public event EventHandler Tick<object>
Public Event EventHandler Tick( Of ( Of object ))
See Also