diff --git a/Xamarin.Essentials/MainThread/MainThread.uwp.cs b/Xamarin.Essentials/MainThread/MainThread.uwp.cs index 45eb8115a..3c1c22da8 100644 --- a/Xamarin.Essentials/MainThread/MainThread.uwp.cs +++ b/Xamarin.Essentials/MainThread/MainThread.uwp.cs @@ -7,13 +7,16 @@ namespace Xamarin.Essentials public static partial class MainThread { static bool PlatformIsMainThread => - CoreApplication.MainView.CoreWindow.Dispatcher == null; + CoreApplication.MainView.CoreWindow?.Dispatcher?.HasThreadAccess ?? false; static void PlatformBeginInvokeOnMainThread(Action action) { - var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; + var dispatcher = CoreApplication.MainView.CoreWindow?.Dispatcher; - if (dispatcher != null) + if (dispatcher == null) + throw new InvalidOperationException("Unable to find main thread."); + + if (!dispatcher.HasThreadAccess) dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).WatchForError(); else action();