MainThread (Xamarin.Essentials API of the Week)

Play MainThread (Xamarin.Essentials API of the Week)

The Discussion

  • User profile image
    Mikilll

    What is the difference to using Device.BeginInvokeOnMainThread() which is built directly into Xamarin.Forms?

  • User profile image
    SmartmanApps
    +1
  • User profile image
    Jeffm
    MiKill - there's a comment in the docs that addresses that....

    Xamarin.Forms has a method called Device.BeginInvokeOnMainThread(Action) that does the same thing as MainThread.BeginInvokeOnMainThread(Action). While you can use either method in a Xamarin.Forms app, consider whether or not the calling code has any other need for a dependency on Xamarin.Forms. If not, MainThread.BeginInvokeOnMainThread(Action) is likely a better option.

    https://docs.microsoft.com/en-us/xamarin/essentials/main-thread
  • User profile image
    briansavage
    Thanks James, great video, simple and to the point, as usual. How about a 101 on threading in general, perhaps async as well. I have a vague idea about how all these work, but your 101s fill in a lot of gaps.
  • User profile image
    chofmeister
    I was curious about updating properties when the UI is bound to properties in MVVM.
    I did a check (MainThread.IsMainThread) when setting properties on a page, and noticed when properties are being updated from events, the property updates are not being updated on the main thread. What is puzzling however, is that the app doesn't throw an exception/crash.

    I am starting to wrap these cases with MainThread.BeginInvokeOnMainThread(() =>{}) but sure would like to understand that better!

Add Your 2 Cents