public void Invoke(Windows.UI.Core.DispatchedHandler handler) { // Validate argument. if (handler == null) { throw new NullReferenceException("handler"); } // Invoke the given handler on the main UI thread now. (This is a block call.) System.Windows.Threading.Dispatcher dispatcher = System.Windows.Deployment.Current.Dispatcher; if (dispatcher.CheckAccess()) { handler.Invoke(); } else if (sIsDispatcherValid) { var waitEvent = new System.Threading.AutoResetEvent(false); var operation = dispatcher.BeginInvoke(() => { handler.Invoke(); waitEvent.Set(); }); waitEvent.WaitOne(); } }
/// <summary> /// Allows updating a view model property from a non-UI thread. An example would be running /// a task in a thread-pool thread that downloads data the view model needs. Once downloaded /// the UI property needs to be updated; however, you cannot access UI elements from a non UI /// thread so you marshal it over using this function which calls the Window Dispatcher to run /// the given task. /// /// Use example: /// InvokeOnUIThread(() => { DelayedAchievementCount = result; }); /// /// </summary> /// <param name="task"> /// The task to update the desired property /// </param> protected void InvokeOnUIThread(Windows.UI.Core.DispatchedHandler task) { if (_syncContext != null && _syncContext != SynchronizationContext.Current) { _syncContext.Post(delegate { task.Invoke(); }, null); } else { task.Invoke(); } }
public static async Task EnsureOnAsync(Windows.UI.Core.DispatchedHandler callback) { if (App.Dispatcher.HasThreadAccess) { callback.Invoke(); } else { await App.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, callback); } }
private void RunOnMainThread(Windows.UI.Core.DispatchedHandler handler) { if (Dispatcher.HasThreadAccess) { handler.Invoke(); } else { // Note: use a discard "_" to silence CS4014 warning _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, handler); } }
public static Task RunOnMainThread(Windows.UI.Core.DispatchedHandler handler) { var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher; if (dispatcher.HasThreadAccess) { handler.Invoke(); return(Task.CompletedTask); } else { return(dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, handler).AsTask()); } }