1

I've found 3 different ways of using the dispatcher

await Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
   // ...
});

await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
   // ...
});

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
   // ...
});

I don't understand the difference between them and when to use which one. My project only has one "page". Which one is the "best" way to go?

Edit// Sorry for this question, currently i'm still learning but didn't found any explanation to this topic.

andy
  • 509
  • 1
  • 8
  • 21
  • There are 2 vote to close for opinon based. It is clear to me that it is not opinion based. If it leads to problems one can reach out a ⯁ user to take care of the mess. – Soleil Mar 06 '21 at 14:02
  • 2
    They are all the same in a basic one-page app. The 1st version is need when taking advantage of the UWP object model not precluding the possibility of creating UI in multiple threads. That's an extremely advanced technique that just about everybody should stay 10,000 miles away from. The 2nd version is necessary when running this code in a worker thread, or a class library that doesn't have a reference to the UI, that ought to be rare. The 3rd version shows confidence in understanding the structure of your app. – Hans Passant Mar 06 '21 at 14:13
  • @HansPassant Thank you very much for your detailed explanation. So for a simple dispatcher, it seems enough (in my case with a single page), to just use the 3rd version? – andy Mar 06 '21 at 17:39

2 Answers2

1

All those are actually instances of the sealed class Dispatcher in the namespace System.Windows.Threading.

From a point of view it is unfortunate to have instances having the same name as the class. From another, it is convenient so one can reach it without looking for the instance name it was given.

Typically you start your background tasks (which otherwise may block your GUI) from the UI thread with Task.Run and from the task, you send your updates to the GUI with the dispatcher:

Task.Run(() =>
{
    // background work
    Application.Current.Dispatcher.Invoke(() =>
    {
        // non blocking UI updates 
    });
}

Using the C# Dispatcher in WPF Applications

Build More Responsive Apps With The Dispatcher

Soleil
  • 6,404
  • 5
  • 41
  • 61
  • The OP's question is specifically asking about UWP and not WPF. Those links you provided, and the code sample, will not work in UWP. – flyte May 19 '22 at 18:06
0

when to use which one

Use none of them.

Instead of having your background threads reach into your UI, have the UI be the "driver" of the app. Use await to get results of asynchronous operations and Progress<T> to report progress.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I have multiple event handlers, which have to update the UI, when they are triggered. My understanding is, that in this case I have to use a dispatcher. – andy Mar 07 '21 at 06:43
  • @andy: In that case, I recommend using a `SynchronizationContext`. – Stephen Cleary Mar 07 '21 at 13:09