67

How can I run code on the UI thread in WinRT (Windows 8 Metro)?

The Invoke method does not exist.

1
  • 8
    Note to future readers: Remember, that if your app has multiple windows - there are multiple UI threads and dispatchers. Feb 17, 2016 at 18:42

6 Answers 6

81

It's easier to directly get the CoreWindow from the non-UI thread. The following code will work everywhere, even when GetForCurrentThread() or Window.Current returns null.

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    <lambda for your code which should run on the UI thread>);

for example:

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    () =>
    {
        // Your UI update code goes here!
    });

You'll need to reference Windows.ApplicationModel.Core namespace:

using Windows.ApplicationModel.Core;
6
  • I get System.NotImplementedException when using this.I access it from UI thread.
    – Naren
    Oct 9, 2013 at 9:29
  • Never had this Exception here. It could be from somewhere else, like inside your block instructions.
    – Cœur
    Oct 9, 2013 at 12:15
  • I get this exception when I tried to access the Dispatcher .I haven't executed any code using the dispatcher.
    – Naren
    Oct 9, 2013 at 12:30
  • So CoreApplication.MainView.CoreWindow.Dispatcher doesn't work for you? Do you have an Exception when trying the other solutions around? Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher? this.Dispatcher?
    – Cœur
    Oct 9, 2013 at 15:48
  • I also tried Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher but that too threw exception.And I guess this.Dispatcher doesn't work with Windows.Runtime.I have posted it as a seperate question.
    – Naren
    Oct 9, 2013 at 16:21
69

Use:

From your UI thread, execute:

var dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;

From your background (non UI thread)

dispatcher.RunAsync(DispatcherPriority.Normal, 
    <lambda for your code which should run on the UI thread>);

That should work on both CP and later builds.

7
  • 5
    Is there a way to get dispatcher on non-UI thread ? Currently i get null from CoreWindow.GetForCurrentThread()
    – Grigory
    Jun 12, 2012 at 14:27
  • 3
    No. Dispatchers are tied to a UI thread, so you need to retrieve the dispatcher on the UI thread. Once the dispatcher has been retrieved, you can remember it. If you're in a XAML application, then most UI objects have dispatcher member that you can use. Jun 14, 2012 at 13:06
  • So which parts of my app are actually running in UI Thread? I'm using a FrameWorkView (Windows::ApplicationModel::Core::IFrameworkView), and not able to use the dispatcher I acquired from Run()-method. I'm getting WrongThreadException when I'm trying to create MediaElement through RunAsync.
    – Habba
    Jul 2, 2012 at 11:55
  • 1
    Your original code runs on the UI thread, as do any of the XAML based event callbacks). If you use the "await" keyword from a UI thread, all the code after the "await" keyword will run on the UI thread. However event callbacks from other WinRT APIs might not be on the UI thread, if they're not, you use the dispatcher to get back to the UI thread. If you get the WrongThreadException, it probably means that the WinRT API came in on a non UI thread. Jul 2, 2012 at 14:02
  • 4
    @Larry: You can get the Dispatcher from the background thread, see here: stackoverflow.com/a/25760799/543303 CoreApplication.MainView.CoreWindow.Dispatcher
    – eFloh
    Sep 10, 2014 at 8:36
8

Use:

this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Frame.Navigate(typeof(Welcome), this));

It works for me.

1
  • 5
    This does not actually guarantee to run it on the UI thread. It only will if "this" is a an object in the UI context.
    – Luke Kim
    Dec 15, 2012 at 0:56
5

This is a much easier way in my opinion.

Get the TaskScheduler associated with the UI.

    var UISyncContext = TaskScheduler.FromCurrentSynchronizationContext();

Then start a new Task and on the above UISyncContext.

    Task.Factory.StartNew(() => { /* Do your UI stuff here; */}, new System.Threading.CancellationToken(), TaskCreationOptions.PreferFairness, UISyncContext);
3
  • FYI: I get an InvalidOperationException "The current SynchronizationContext may not be used as a TaskScheduler".
    – Akku
    Jan 2, 2013 at 11:23
  • I also get an InvalidOperation
    – LowDev1
    Jun 4, 2013 at 20:21
  • 1
    Your SynchronizationContext is null. That's why you are getting that exception. Nothing wrong with my code.
    – Deeb
    Jul 30, 2013 at 14:24
0

DispatcherTimer is also an option.

I used it for code that must be run in Xaml-designer (CoreWindow.Dispatcher,... are not available in UWP-designer)

var localTimer = new DispatcherTimer
{
    Interval = TimeSpan.FromMilliseconds(0)
};
localTimer.Tick += (timer, e) =>
{
    (timer as DispatcherTimer).Stop();
    action();
};
localTimer.Start();

Disclaimer:
I should note that this should be a last-resort option if every other fails.

0

On UWP, I was having problem trying to set the Source property of CaptureElement control (that is defined in XAML), it was complaining about being prepared at different thread, even though I was trying to set it from code that was invoked via a Page_Loaded event handler. I ended up using this to work around it:

previewControl.Dispatcher.TryRunAsync(CoreDispatcherPriority.Normal, () => {
   previewControl.Source = _mediaCapture;
}).GetAwaiter().GetResult();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.