MAUI Equivalent for Dispatcher.Run

165 views Asked by At

In WPF applications, I could create a new thread, get the Systems.Windows.Dispatcher for that thread, and call Dispatcher.Run to get dispatcher services for the new thread. If I call Dispatcher.GetForCurrentThread() on a newly-created thread in MAUI, it just returns a null. Is there a similar capability in MAUI for dispatcher-like operations on a newly-created thread?

1

There are 1 answers

2
Jianwei Sun - MSFT On

For MAUI, there are several ways to use:

  • BindableObject.Dispatcher.Dispatch()
  • Dispatcher.GetForCurrentThread().Dispatch()
  • MainThread.BeginInvokeOnMainThread()

About their specific differences, You can refer to mattleibow's answer on GitHub, and he said:

There is also a Dispatcher.GetForCurrentThread() that can be used. This is different to MainThread in that MainThread will always try find a UI thread. GetForCurrentThread will return null if the current thread has no dispatcher.

In addition, you can also see this answer of ToolmakerSteve on so: For Dotnet Maui, what the difference between Application.Current?.Dispatcher.Dispatch(async()) and MainThread.InvokeOnMainThreadAsync.

Wish these can help you :-)