using System; using System.Runtime.CompilerServices; using Windows.UI.Core; namespace OwlCore.WinUI.Extensions { /// /// Provides extensions for /// public static class CoreDispatcherExtensions { /// /// Continues the current async method on the UI thread. /// /// The CoreDispatcher to use for dispatching. /// public static SwitchToUIAwaitable SwitchToUI(this CoreDispatcher dispatcher) { return new SwitchToUIAwaitable(dispatcher); } /// /// An extension to switch to the UI thread. /// public readonly struct SwitchToUIAwaitable : INotifyCompletion { private readonly CoreDispatcher _dispatcher; /// /// Creates a new instance of . /// /// public SwitchToUIAwaitable(CoreDispatcher dispatcher) { _dispatcher = dispatcher; } /// /// Gets the current awaiter. /// public SwitchToUIAwaitable GetAwaiter() { return this; } /// /// Gets the result of the task. /// public void GetResult() { } /// /// Gets the current completion state. /// public bool IsCompleted => _dispatcher.HasThreadAccess; /// /// Fires when the task is completed. /// /// The action to perform when continuing. public void OnCompleted(Action continuation) { _ = _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => continuation()); } } } }