// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Threading { using System; using System.Threading; using System.Threading.Tasks; using Perspex.Win32.Threading; /// /// Provides services for managing work items on a thread. /// /// /// In Perspex, there is usually only a single in the application - /// the one for the UI thread, retrieved via the property. /// public class Dispatcher { private static Dispatcher instance = new Dispatcher(); private MainLoop mainLoop = new MainLoop(); /// /// Initializes a new instance of the class. /// private Dispatcher() { } /// /// Gets the for the UI thread. /// public static Dispatcher UIThread { get { return instance; } } /// /// Runs the dispatcher's main loop. /// /// /// A cancellation token used to exit the main loop. /// public void MainLoop(CancellationToken cancellationToken) { this.mainLoop.Run(cancellationToken); } /// /// Invokes a method on the dispatcher thread. /// /// The method. /// The priority with which to invoke the method. /// A task that can be used to track the method's execution. public Task InvokeAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal) { return this.mainLoop.InvokeAsync(action, priority); } } }