Share via


DispatcherQueueTimer Class

Definition

Periodically executes a task on a DispatcherQueue thread after a time interval has elapsed.

public ref class DispatcherQueueTimer sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 327680)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class DispatcherQueueTimer final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 327680)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class DispatcherQueueTimer final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 327680)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class DispatcherQueueTimer
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 327680)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class DispatcherQueueTimer
Public NotInheritable Class DispatcherQueueTimer
Inheritance
Object Platform::Object IInspectable DispatcherQueueTimer
Attributes

Windows requirements

Device family
Windows 10 Fall Creators Update (introduced in 10.0.16299.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v5.0)

Examples

public void ConfigureRepeatingTimer()
{
    _queueController = DispatcherQueueController.CreateOnDedicatedThread();
    _queue = _queueController.DispatcherQueue;

    _repeatingTimer = _queue.CreateTimer ();
    _repeatingTimer.Interval = TimeSpan.FromSeconds(5);

    // The tick handler will be invoked repeatedly after every 5
    // seconds on the dedicated thread.
    _repeatingTimer.Tick += (s, e) =>
    {
        DoWork();
    };

    // Start the Timer
    _repeatingTimer.Start();
}

private DispatcherQueue _queue;
private DispatcherQueueController _queueController;
private DispatcherQueueTimer _repeatingTimer;

Remarks

The system guarantees to invoke the event handler only after the specified duration expires. However, there may be some delay before the tick handler is invoked if there are other pending work items in the queue.

Timer tasks run at a priority lower than idle.

Timers don’t keep the DispatcherQueue event loop alive. Timers created after the DispatcherQueue event loop has stopped will not be processed.

Properties

Interval

Gets and sets the interval for the timer.

IsRepeating

Indicates whether the timer is repeating.

IsRunning

Indicates whether the timer is currently running.

Methods

Start()

Starts the timer.

Stop()

Stops the timer.

Events

Tick

Event that fires when the timer Interval elapses.

Applies to