Skip to main content

Dispatcher Class

Definition​

Assembly:Avalonia.Base
Package:Avalonia

Provides services for managing work items on a thread.

public class Dispatcher

Inheritance: object -> Dispatcher

Implements:IDispatcher

Methods​

NameDescription
AwaitWithPriority (2 overloads)Returns a task awaitable that would invoke continuation on specified dispatcher priority
BeginInvokeShutdownBegins the process of shutting down the dispatcher.
CheckAccessChecks that the current thread is the UI thread.
DisableProcessingDisable the event processing of the dispatcher.
ExitAllFramesRequests that all nested frames exit.
FromThreadNo summary available.
HasJobsWithPriorityNo summary available.
Invoke (8 overloads)Executes the specified Action synchronously on the thread that the Dispatcher was created on.
InvokeAsync (10 overloads)Executes the specified Action asynchronously on the thread that the Dispatcher was created on.
InvokeShutdownInitiates the shutdown process of the Dispatcher synchronously.
MainLoopRuns the dispatcher's main loop.
Post (2 overloads)Posts an action that will be invoked on the dispatcher thread.
PushFramePush an execution frame.
Resume (2 overloads)Creates an awaitable object that asynchronously resumes execution on the dispatcher.
RunJobsNo summary available.
ToTaskScheduler (2 overloads)Gets a System.Threading.Tasks.TaskScheduler which executes tasks on this Avalonia.Threading.Dispatcher. A Avalonia.Threading.DispatcherPriority is captured from the current Avalonia.Threading.AvaloniaSynchronizationContext if one is available. Otherwise, Avalonia.Threading.DispatcherPriority.Default is used.
VerifyAccessChecks that the current thread is the UI thread and throws if not.
Yield (2 overloads)Creates an awaitable object that asynchronously yields control back to the current dispatcher and provides an opportunity for the dispatcher to process other events.

AwaitWithPriority overloads​

AwaitWithPriority Method​

Returns a task awaitable that would invoke continuation on specified dispatcher priority

public Avalonia.Threading.DispatcherPriorityAwaitable AwaitWithPriority(System.Threading.Tasks.Task task, Avalonia.Threading.DispatcherPriority priority)
Parameters​

task System.Threading.Tasks.Task

priority Avalonia.Threading.DispatcherPriority

Returns​

Avalonia.Threading.DispatcherPriorityAwaitable

AwaitWithPriority Method​

public Avalonia.Threading.DispatcherPriorityAwaitable<T><T> AwaitWithPriority<T>(System.Threading.Tasks.Task<T> task, Avalonia.Threading.DispatcherPriority priority)
Parameters​

task System.Threading.Tasks.Task<T>

priority Avalonia.Threading.DispatcherPriority

Type Parameters​

T

Returns​

Avalonia.Threading.DispatcherPriorityAwaitable<T><T>

BeginInvokeShutdown Method​

Begins the process of shutting down the dispatcher.

public void BeginInvokeShutdown(Avalonia.Threading.DispatcherPriority priority)

Parameters​

priority Avalonia.Threading.DispatcherPriority

CheckAccess Method​

Checks that the current thread is the UI thread.

public bool CheckAccess()

Returns​

bool

DisableProcessing Method​

Disable the event processing of the dispatcher.

public Avalonia.Threading.Dispatcher.DispatcherProcessingDisabled DisableProcessing()

Returns​

Avalonia.Threading.Dispatcher.DispatcherProcessingDisabled

Remarks​

This is an advanced method intended to eliminate the chance of unrelated reentrancy. The effect of disabling processing is:

  1. CLR locks will not pump messages internally.
  2. No one is allowed to push a frame.
  3. No message processing is permitted.

ExitAllFrames Method​

Requests that all nested frames exit.

public void ExitAllFrames()

FromThread Method​

public Avalonia.Threading.Dispatcher FromThread(System.Threading.Thread thread)

Parameters​

thread System.Threading.Thread

Returns​

Avalonia.Threading.Dispatcher

HasJobsWithPriority Method​

public bool HasJobsWithPriority(Avalonia.Threading.DispatcherPriority priority)

Parameters​

priority Avalonia.Threading.DispatcherPriority

Returns​

bool

Invoke overloads​

Invoke Method​

Executes the specified Action synchronously on the thread that the Dispatcher was created on.

public void Invoke(Action callback)
Parameters​

callback Action

An Action delegate to invoke through the dispatcher.

Remarks​

Note that the default priority is DispatcherPriority.Send.

Invoke Method​

Executes the specified Action synchronously on the thread that the Dispatcher was created on.

public void Invoke(Action callback, Avalonia.Threading.DispatcherPriority priority)
Parameters​

callback Action

An Action delegate to invoke through the dispatcher.

priority Avalonia.Threading.DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

Invoke Method​

Executes the specified Action synchronously on the thread that the Dispatcher was created on.

public void Invoke(Action callback, Avalonia.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken)
Parameters​

callback Action

An Action delegate to invoke through the dispatcher.

priority Avalonia.Threading.DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

cancellationToken System.Threading.CancellationToken

A cancellation token that can be used to cancel the operation. If the operation has not started, it will be aborted when the cancellation token is canceled. If the operation has started, the operation can cooperate with the cancellation request.

Invoke Method​

Executes the specified Action synchronously on the thread that the Dispatcher was created on.

public void Invoke(Action callback, Avalonia.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken, TimeSpan timeout)
Parameters​

callback Action

An Action delegate to invoke through the dispatcher.

priority Avalonia.Threading.DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

cancellationToken System.Threading.CancellationToken

A cancellation token that can be used to cancel the operation. If the operation has not started, it will be aborted when the cancellation token is canceled. If the operation has started, the operation can cooperate with the cancellation request.

timeout TimeSpan

The minimum amount of time to wait for the operation to start. Once the operation has started, it will complete before this method returns.

Invoke Method​

public TResult Invoke<TResult>(Func<TResult> callback)
Parameters​

callback Func<TResult>

Type Parameters​

TResult

Returns​

TResult

Invoke Method​

public TResult Invoke<TResult>(Func<TResult> callback, Avalonia.Threading.DispatcherPriority priority)
Parameters​

callback Func<TResult>

priority Avalonia.Threading.DispatcherPriority

Type Parameters​

TResult

Returns​

TResult

Invoke Method​

public TResult Invoke<TResult>(Func<TResult> callback, Avalonia.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken)
Parameters​

callback Func<TResult>

priority Avalonia.Threading.DispatcherPriority

cancellationToken System.Threading.CancellationToken

Type Parameters​

TResult

Returns​

TResult

Invoke Method​

public TResult Invoke<TResult>(Func<TResult> callback, Avalonia.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken, TimeSpan timeout)
Parameters​

callback Func<TResult>

priority Avalonia.Threading.DispatcherPriority

cancellationToken System.Threading.CancellationToken

timeout TimeSpan

Type Parameters​

TResult

Returns​

TResult

InvokeAsync overloads​

InvokeAsync Method​

Executes the specified Action asynchronously on the thread that the Dispatcher was created on.

public Avalonia.Threading.DispatcherOperation InvokeAsync(Action callback)
Parameters​

callback Action

An Action delegate to invoke through the dispatcher.

Returns​

Avalonia.Threading.DispatcherOperation

An operation representing the queued delegate to be invoked.

Remarks​

Note that the default priority is DispatcherPriority.Default.

InvokeAsync Method​

Executes the specified Action asynchronously on the thread that the Dispatcher was created on.

public Avalonia.Threading.DispatcherOperation InvokeAsync(Action callback, Avalonia.Threading.DispatcherPriority priority)
Parameters​

callback Action

An Action delegate to invoke through the dispatcher.

priority Avalonia.Threading.DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

Returns​

Avalonia.Threading.DispatcherOperation

An operation representing the queued delegate to be invoked.

InvokeAsync Method​

Executes the specified Action asynchronously on the thread that the Dispatcher was created on.

public Avalonia.Threading.DispatcherOperation InvokeAsync(Action callback, Avalonia.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken)
Parameters​

callback Action

An Action delegate to invoke through the dispatcher.

priority Avalonia.Threading.DispatcherPriority

The priority that determines in what order the specified callback is invoked relative to the other pending operations in the Dispatcher.

cancellationToken System.Threading.CancellationToken

A cancellation token that can be used to cancel the operation. If the operation has not started, it will be aborted when the cancellation token is canceled. If the operation has started, the operation can cooperate with the cancellation request.

Returns​

Avalonia.Threading.DispatcherOperation

An operation representing the queued delegate to be invoked.

InvokeAsync Method​

public System.Threading.Tasks.Task InvokeAsync(Func<System.Threading.Tasks.Task> callback)
Parameters​

callback Func<System.Threading.Tasks.Task>

Returns​

System.Threading.Tasks.Task

InvokeAsync Method​

public System.Threading.Tasks.Task InvokeAsync(Func<System.Threading.Tasks.Task> callback, Avalonia.Threading.DispatcherPriority priority)
Parameters​

callback Func<System.Threading.Tasks.Task>

priority Avalonia.Threading.DispatcherPriority

Returns​

System.Threading.Tasks.Task

InvokeAsync Method​

public System.Threading.Tasks.Task<TResult> InvokeAsync<TResult>(Func<System.Threading.Tasks.Task<TResult>> action)
Parameters​

action Func<System.Threading.Tasks.Task<TResult>>

Type Parameters​

TResult

Returns​

System.Threading.Tasks.Task<TResult>

InvokeAsync Method​

public System.Threading.Tasks.Task<TResult> InvokeAsync<TResult>(Func<System.Threading.Tasks.Task<TResult>> action, Avalonia.Threading.DispatcherPriority priority)
Parameters​

action Func<System.Threading.Tasks.Task<TResult>>

priority Avalonia.Threading.DispatcherPriority

Type Parameters​

TResult

Returns​

System.Threading.Tasks.Task<TResult>

InvokeAsync Method​

public Avalonia.Threading.DispatcherOperation<T><TResult> InvokeAsync<TResult>(Func<TResult> callback)
Parameters​

callback Func<TResult>

Type Parameters​

TResult

Returns​

Avalonia.Threading.DispatcherOperation<T><TResult>

InvokeAsync Method​

public Avalonia.Threading.DispatcherOperation<T><TResult> InvokeAsync<TResult>(Func<TResult> callback, Avalonia.Threading.DispatcherPriority priority)
Parameters​

callback Func<TResult>

priority Avalonia.Threading.DispatcherPriority

Type Parameters​

TResult

Returns​

Avalonia.Threading.DispatcherOperation<T><TResult>

InvokeAsync Method​

public Avalonia.Threading.DispatcherOperation<T><TResult> InvokeAsync<TResult>(Func<TResult> callback, Avalonia.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken)
Parameters​

callback Func<TResult>

priority Avalonia.Threading.DispatcherPriority

cancellationToken System.Threading.CancellationToken

Type Parameters​

TResult

Returns​

Avalonia.Threading.DispatcherOperation<T><TResult>

InvokeShutdown Method​

Initiates the shutdown process of the Dispatcher synchronously.

public void InvokeShutdown()

MainLoop Method​

Runs the dispatcher's main loop.

public void MainLoop(System.Threading.CancellationToken cancellationToken)

Parameters​

cancellationToken System.Threading.CancellationToken

A cancellation token used to exit the main loop.

Post overloads​

Post Method​

Posts an action that will be invoked on the dispatcher thread.

public void Post(Action action, Avalonia.Threading.DispatcherPriority priority)
Parameters​

action Action

The method.

priority Avalonia.Threading.DispatcherPriority

The priority with which to invoke the method.

Post Method​

Posts an action that will be invoked on the dispatcher thread.

public void Post(System.Threading.SendOrPostCallback action, object arg, Avalonia.Threading.DispatcherPriority priority)
Parameters​

action System.Threading.SendOrPostCallback

The method.

arg object

The argument of method to call.

priority Avalonia.Threading.DispatcherPriority

The priority with which to invoke the method.

PushFrame Method​

Push an execution frame.

public void PushFrame(Avalonia.Threading.DispatcherFrame frame)

Parameters​

frame Avalonia.Threading.DispatcherFrame

The frame for the dispatcher to process.

Resume overloads​

Resume Method​

Creates an awaitable object that asynchronously resumes execution on the dispatcher.

public Avalonia.Threading.DispatcherPriorityAwaitable Resume()
Returns​

Avalonia.Threading.DispatcherPriorityAwaitable

An awaitable object that asynchronously resumes execution on the dispatcher.

Remarks​

This method is equivalent to calling the Avalonia.Threading.Dispatcher.Resume(Avalonia.Threading.DispatcherPriority) method and passing in Avalonia.Threading.DispatcherPriority.Background.

Resume Method​

`` Creates an awaitable object that asynchronously resumes execution on the dispatcher. The work that occurs when control returns to the code awaiting the result of this method is scheduled with the specified priority.

public Avalonia.Threading.DispatcherPriorityAwaitable Resume(Avalonia.Threading.DispatcherPriority priority)
Parameters​

priority Avalonia.Threading.DispatcherPriority

The priority at which to schedule the continuation.

Returns​

Avalonia.Threading.DispatcherPriorityAwaitable

An awaitable object that asynchronously resumes execution on the dispatcher.

RunJobs Method​

public void RunJobs(Nullable<Avalonia.Threading.DispatcherPriority> priority)

Parameters​

priority Nullable<Avalonia.Threading.DispatcherPriority>

ToTaskScheduler overloads​

ToTaskScheduler Method​

Gets a System.Threading.Tasks.TaskScheduler which executes tasks on this Avalonia.Threading.Dispatcher. A Avalonia.Threading.DispatcherPriority is captured from the current Avalonia.Threading.AvaloniaSynchronizationContext if one is available. Otherwise, Avalonia.Threading.DispatcherPriority.Default is used.

public System.Threading.Tasks.TaskScheduler ToTaskScheduler()
Returns​

System.Threading.Tasks.TaskScheduler

ToTaskScheduler Method​

Gets a System.Threading.Tasks.TaskScheduler which executes tasks on this Avalonia.Threading.Dispatcher with the specified Avalonia.Threading.DispatcherPriority.

public System.Threading.Tasks.TaskScheduler ToTaskScheduler(Avalonia.Threading.DispatcherPriority priority)
Parameters​

priority Avalonia.Threading.DispatcherPriority

Returns​

System.Threading.Tasks.TaskScheduler

VerifyAccess Method​

Checks that the current thread is the UI thread and throws if not.

public void VerifyAccess()

Exceptions​

Yield overloads​

Yield Method​

Creates an awaitable object that asynchronously yields control back to the current dispatcher and provides an opportunity for the dispatcher to process other events.

public Avalonia.Threading.DispatcherPriorityAwaitable Yield()
Returns​

Avalonia.Threading.DispatcherPriorityAwaitable

An awaitable object that asynchronously yields control back to the current dispatcher and provides an opportunity for the dispatcher to process other events.

Remarks​

This method is equivalent to calling the Avalonia.Threading.Dispatcher.Yield(Avalonia.Threading.DispatcherPriority) method and passing in Avalonia.Threading.DispatcherPriority.Background.

Exceptions​

Yield Method​

Creates an cawaitable object that asynchronously yields control back to the current dispatcher and provides an opportunity for the dispatcher to process other events. The work that occurs when control returns to the code awaiting the result of this method is scheduled with the specified priority.

public Avalonia.Threading.DispatcherPriorityAwaitable Yield(Avalonia.Threading.DispatcherPriority priority)
Parameters​

priority Avalonia.Threading.DispatcherPriority

The priority at which to schedule the continuation.

Returns​

Avalonia.Threading.DispatcherPriorityAwaitable

An awaitable object that asynchronously yields control back to the current dispatcher and provides an opportunity for the dispatcher to process other events.

Exceptions​

Properties​

NameDescription
CurrentDispatcherNo summary available.
SupportsRunLoopsNo summary available.
ThreadNo summary available.
UIThreadGets the dispatcher for the UI thread.

CurrentDispatcher Property​

public Avalonia.Threading.Dispatcher CurrentDispatcher { get; set; }

SupportsRunLoops Property​

public bool SupportsRunLoops { get; set; }

Thread Property​

public System.Threading.Thread Thread { get; set; }

UIThread Property​

Gets the dispatcher for the UI thread.

public Avalonia.Threading.Dispatcher UIThread { get; set; }

Remarks​

Control and libraries author are encouraged to use Avalonia.Threading.Dispatcher.CurrentDispatcher and Avalonia.AvaloniaObject.Dispatcher instead.

Events​

NameDescription
ShutdownFinishedRaised when the dispatcher is shut down.
ShutdownStartedRaised when the dispatcher is shutting down.
UnhandledExceptionOccurs when a thread exception is thrown and uncaught during execution of a delegate by way of Avalonia.Threading.Dispatcher.Invoke(Action) or Avalonia.Threading.Dispatcher.InvokeAsync(Action).
UnhandledExceptionFilterOccurs when a thread exception is thrown and uncaught during execution of a delegate by way of Avalonia.Threading.Dispatcher.Invoke(Action) or Avalonia.Threading.Dispatcher.InvokeAsync(Action) when in the filter stage.

ShutdownFinished Event​

Raised when the dispatcher is shut down.

public event EventHandler ShutdownFinished

ShutdownStarted Event​

Raised when the dispatcher is shutting down.

public event EventHandler ShutdownStarted

UnhandledException Event​

Occurs when a thread exception is thrown and uncaught during execution of a delegate by way of Avalonia.Threading.Dispatcher.Invoke(Action) or Avalonia.Threading.Dispatcher.InvokeAsync(Action).

public event Avalonia.Threading.DispatcherUnhandledExceptionEventHandler UnhandledException

Remarks​

This event is raised when an exception that was thrown during execution of a delegate by way of Avalonia.Threading.Dispatcher.Invoke(Action) or Avalonia.Threading.Dispatcher.InvokeAsync(Action) is uncaught. A handler can mark the exception as handled, which will prevent the internal exception handler from being called. Event handlers for this event must be written with care to avoid creating secondary exceptions and to catch any that occur. It is recommended to avoid allocating memory or doing any resource intensive operations in the handler.

UnhandledExceptionFilter Event​

Occurs when a thread exception is thrown and uncaught during execution of a delegate by way of Avalonia.Threading.Dispatcher.Invoke(Action) or Avalonia.Threading.Dispatcher.InvokeAsync(Action) when in the filter stage.

public event Avalonia.Threading.DispatcherUnhandledExceptionFilterEventHandler UnhandledExceptionFilter

Remarks​

This event is raised during the filter stage for an exception that is raised during execution of a delegate by way of Avalonia.Threading.Dispatcher.Invoke(Action) or Avalonia.Threading.Dispatcher.InvokeAsync(Action) and is uncaught. The call stack is not unwound at this point (first-chance exception). Event handlers for this event must be written with care to avoid creating secondary exceptions and to catch any that occur. It is recommended to avoid allocating memory or doing any resource intensive operations in the handler. The Avalonia.Threading.Dispatcher.UnhandledExceptionFilter event provides a means to not raise the Avalonia.Threading.Dispatcher.UnhandledException event. The Avalonia.Threading.Dispatcher.UnhandledExceptionFilter event is raised first, and If Avalonia.Threading.DispatcherUnhandledExceptionFilterEventArgs.RequestCatch is set to false, the Avalonia.Threading.Dispatcher.UnhandledException event will not be raised.