Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Dispatcher in WinRT?

I have some WP7 code that looks like this:



using System.Windows;
using System.Windows.Threading;

public static class GlobalDispatcher
{
    public static Dispatcher Current { get { return Deployment.Current.Dispatcher; } }
}

What is the equivalent in WinRT? Is there no globally accessible Dispatcher (or CoreDispatcher) object?

like image 314
Arash Emami Avatar asked Nov 20 '12 21:11

Arash Emami


3 Answers

You will want to use Window.Current.Dispatcher.

like image 55
Jeff Brand Avatar answered Oct 20 '22 16:10

Jeff Brand


Just adding this as answer, as it better solves the problem in my opinion:

Window.Current is null if the application is not focused and thus will bring you in trouble when there is a user involved ;)

CoreApplication.MainView.CoreWindow.Dispatcher isn't null...

– Jeremy White Aug 4 '13 at 13:09

like image 39
eFloh Avatar answered Oct 20 '22 16:10

eFloh


try this:

        var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
        await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            try
            {
               //Your code here
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        });
like image 21
Vishal Avatar answered Oct 20 '22 15:10

Vishal