namespace VideoBrowser.Helpers { using System; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; /// /// Defines the . /// public static class UIThreadHelper { #region Methods /// /// The DelayedInvokeAsync. /// /// The action. /// The milliSecondsDelay. public static void DelayedInvokeAsync(Action action, int milliSecondsDelay) { Task.Run(async () => { await Task.Delay(milliSecondsDelay); InvokeAsync(action); }); } /// /// The Invoke. /// /// The action. public static void Invoke(Action action) { if (Application.Current == null) { return; } var dispatcher = Application.Current.Dispatcher; if (dispatcher.CheckAccess()) { action?.Invoke(); } else { dispatcher.Invoke(() => action?.Invoke()); } } /// /// The Invoke. /// /// The dispatcherObject. /// The action. public static void Invoke(this DispatcherObject dispatcherObject, Action action) { var dispatcher = dispatcherObject.Dispatcher; if (dispatcher.CheckAccess()) { action?.Invoke(); } else { dispatcher.Invoke(() => action?.Invoke()); } } /// /// The InvokeAsync. /// /// The action. public static void InvokeAsync(Action action) { if (Application.Current == null) { return; } Application.Current.Dispatcher.InvokeAsync(action); } /// /// The InvokeAsync. /// /// The dispatcher. /// The action. public static void InvokeAsync(this Dispatcher dispatcher, Action action) { if (dispatcher.CheckAccess()) { action?.Invoke(); } else { dispatcher.InvokeAsync(() => action?.Invoke()); } } /// /// The InvokeAsync. /// /// The dispatcherObject. /// The action. public static void InvokeAsync(this DispatcherObject dispatcherObject, Action action) { dispatcherObject.Dispatcher.InvokeAsync(action); } #endregion Methods } }