DispatcherTimer class

Expand
0 out of 1 rated this helpful - Rate this topic

DispatcherTimer class

[This documentation is preliminary and is subject to change.]

Applies to: Metro style apps only

Provides a timer that is integrated into the Dispatcher queue, which is processed at a specified interval of time and at a specified priority. One scenario for this is to run code on the UI thread.

Inheritance

Object
  DispatcherTimer

Syntax


public class DispatcherTimer : Object

Attributes

MarshalingBehaviorAttribute(Agile)
ThreadingAttribute(Both)
VersionAttribute(NTDDI_WIN8)
WebHostHiddenAttribute()

Members

The DispatcherTimer class has these types of members:

Constructors

The DispatcherTimer class has these constructors.

ConstructorDescription
DispatcherTimer Initializes a new instance of the DispatcherTimer class.

 

Events

The DispatcherTimer class has these events.

EventDescription
Tick Occurs when the timer interval has elapsed.

 

Methods

The DispatcherTimer class has these methods. It also inherits methods from the Object class.

MethodDescription
Start Starts the DispatcherTimer.
Stop Stops the DispatcherTimer.

 

Properties

The DispatcherTimer class has these properties.

PropertyAccess typeDescription

Interval

Read/writeGets or sets the amount of time between timer ticks.

IsEnabled

Read-onlyGets a value that indicates whether the timer is running.

 

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).

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();
        }


Requirements

Minimum supported client

Windows 8 Release Preview

Minimum supported server

Windows Server 2012

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

See also

CoreDispatcher
Object

 

 

Build date: 5/22/2012

Did you find this helpful?
(1500 characters remaining)
Community Additions ADD