Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Properly check for thread access on UWP (#332)
Browse files Browse the repository at this point in the history
* Properly check for thread access on UWP
* Do some null checks with UWP
* #292 seems to have been lost
  • Loading branch information
mattleibow authored Jun 26, 2018
1 parent 37bb821 commit 7e3e6a7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Xamarin.Essentials/MainThread/MainThread.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7e3e6a7

Please sign in to comment.