Table of Contents

Class DispatcherTimer

Namespace
Avalonia.Threading
Assembly
Avalonia.Base.dll

A timer that is integrated into the Dispatcher queues, and will be processed after a given amount of time at a specified priority.

public class DispatcherTimer
Inheritance
DispatcherTimer
Inherited Members

Constructors

DispatcherTimer()

Creates a timer that uses theUI thread's Dispatcher2 to process the timer event at background priority.

public DispatcherTimer()

DispatcherTimer(DispatcherPriority)

Creates a timer that uses the UI thread's Dispatcher2 to process the timer event at the specified priority.

public DispatcherTimer(DispatcherPriority priority)

Parameters

priority DispatcherPriority

The priority to process the timer at.

DispatcherTimer(TimeSpan, DispatcherPriority, EventHandler)

Creates a timer that uses the UI thread's Dispatcher2 to process the timer event at the specified priority after the specified timeout.

public DispatcherTimer(TimeSpan interval, DispatcherPriority priority, EventHandler callback)

Parameters

interval TimeSpan

The interval to tick the timer after.

priority DispatcherPriority

The priority to process the timer at.

callback EventHandler

The callback to call when the timer ticks.

Properties

Dispatcher

Gets the dispatcher this timer is associated with.

public Dispatcher Dispatcher { get; }

Property Value

Dispatcher

Interval

Gets or sets the time between timer ticks.

public TimeSpan Interval { get; set; }

Property Value

TimeSpan

IsEnabled

Gets or sets whether the timer is running.

public bool IsEnabled { get; set; }

Property Value

bool

Tag

Any data that the caller wants to pass along with the timer.

public object? Tag { get; set; }

Property Value

object

Methods

Run(Func<bool>, TimeSpan, DispatcherPriority)

Starts a new timer.

public static IDisposable Run(Func<bool> action, TimeSpan interval, DispatcherPriority priority = default)

Parameters

action Func<bool>

The method to call on timer tick. If the method returns false, the timer will stop.

interval TimeSpan

The interval at which to tick.

priority DispatcherPriority

The priority to use.

Returns

IDisposable

An IDisposable used to cancel the timer.

RunOnce(Action, TimeSpan, DispatcherPriority)

Runs a method once, after the specified interval.

public static IDisposable RunOnce(Action action, TimeSpan interval, DispatcherPriority priority = default)

Parameters

action Action

The method to call after the interval has elapsed.

interval TimeSpan

The interval after which to call the method.

priority DispatcherPriority

The priority to use.

Returns

IDisposable

An IDisposable used to cancel the timer.

Start()

Starts the timer.

public void Start()

Stop()

Stops the timer.

public void Stop()

Events

Tick

Occurs when the specified timer interval has elapsed and the timer is enabled.

public event EventHandler? Tick

Event Type

EventHandler