'Migrating DispatcherHelper from MVVMLight to Windows Community Toolkit

I am working on upgrading .NET 4.5 based WPF application to .Net 6. The current application uses DispatcherHelper which is not available in Windows Community Toolkit. I am unable to find any sample code which can help me to replace it with the ones available in WCT. I want to update the following code block with the one available in Windows Community Toolkit:

DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
    this.SelectedControl = new View.MainMenu();
});

and

DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
     this.SelectedControl = new View.Project();
});


Solution 1:[1]

You can find the source code for the DispatcherHelper here. In essence it does:

  • Check if the action is valid, not null.
  • Check if the calling thread is already the UI thread and if so, executes the action directly, otherwise queues it for asynchronous exectution.
public static class DispatcherHelper
{
   public static void CheckBeginInvokeOnUI(Action action)
   {
      if (action == null)
         return;

      var dispatcher = Application.Current.Dispatcher;
      if (dispatcher.CheckAccess())
         action();
      else
         dispatcher.BeginInvoke(action);
   }
}

As far as I can see, there is only a replacement for DispatcherHelper for UWP in the toolkit, but not for WPF. Either you replace the calls using your own DispatcherHelper type as above or you research where and in which context the method is used. You might as well be able to replace synchronous calls to the dispatcher with Invoke and asynchronous calls with InvokeAsync.


The above version of the DispatcherHelper is cut to the essentials. In full, it would look like below for WPF. The only thing that it provides more is that you can manually initialize the dispatcher (plus check if it is alive), reset it, and as well as directly queueing an action for asynchronous execution on the dispatcher, but it might not be of any use for you.

public static class DispatcherHelper
{
   public static Dispatcher UIDispatcher { get; private set; }

   public static void CheckBeginInvokeOnUI(Action action)
   {
      if (action == null)
         return;

      CheckDispatcher();

      if (UIDispatcher.CheckAccess())
         action();
      else
         UIDispatcher.BeginInvoke(action);
   }

   private static void CheckDispatcher()
   {
      if (UIDispatcher == null)
         throw new InvalidOperationException("The DispatcherHelper is not initialized.\n" +
                                             "Call DispatcherHelper.Initialize() in the static App constructor.");
   }

   public static DispatcherOperation RunAsync(Action action)
   {
      CheckDispatcher();
      return UIDispatcher.BeginInvoke(action);
   }

   public static void Initialize()
   {
      if (UIDispatcher != null && UIDispatcher.Thread.IsAlive)
         return;

      UIDispatcher = Dispatcher.CurrentDispatcher;
   }

   public static void Reset()
   {
      UIDispatcher = null;
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 thatguy