示例#1
0
        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();
            }
        }
示例#2
0
 /// <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();
     }
 }
示例#3
0
 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);
     }
 }
示例#5
0
        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());
            }
        }