Skip to content
Permalink
Browse files

Update SingleWindowDispatcherScheduler.cs (#2251)

type: fix 
accessing CoreApplication.Views throws in XamlIslands, thus in this case get the CoreDispatcher by alternate means

issue: #2154
  • Loading branch information...
jasonwurzel authored and glennawatson committed Nov 20, 2019
1 parent 857eb2a commit 02e932d33a9dc1c104537ded99d42c48159a1c83
Showing with 22 additions and 4 deletions.
  1. +22 −4 src/ReactiveUI/Platforms/uap/SingleWindowDispatcherScheduler.cs
@@ -38,10 +38,24 @@ public class SingleWindowDispatcherScheduler : IScheduler
/// </summary>
public SingleWindowDispatcherScheduler()
{
if (CoreApplication.Views.Count > 0)
if (CoreApplication.Views.Count == 0)
{
return;
}

CoreDispatcher coreDispatcher;

try
{
Interlocked.CompareExchange(ref _dispatcher, CoreApplication.Views[0].Dispatcher, null);
coreDispatcher = CoreApplication.Views[0].Dispatcher;
}
catch
{
// in the XamlIsland case, accessing Views throws. Thus, falling back to the old way. This HAS to be initialized on the MainThread.
coreDispatcher = Window.Current.Dispatcher;
}

Interlocked.CompareExchange(ref _dispatcher, coreDispatcher, null);
}

/// <summary>
@@ -152,9 +166,13 @@ private IDisposable ScheduleOnDispatcherNow<TState>(TState state, Func<ISchedule
{
try
{
Interlocked.CompareExchange(ref _dispatcher, CoreApplication.Views.FirstOrDefault()?.Dispatcher, null);
// if _dispatcher is still null (and only then) CompareExchange it with the dispatcher from the first view found
if (_dispatcher == null)
{
Interlocked.CompareExchange(ref _dispatcher, CoreApplication.Views.FirstOrDefault()?.Dispatcher, null);
}
}
catch (Exception ex)
catch
{
// Ignore
}

0 comments on commit 02e932d

Please sign in to comment.
You can’t perform that action at this time.