Skip to content

Commit

Permalink
fix(dispatcherqueue): Use proper CheckThreadAccess/`HasThreadAccess…
Browse files Browse the repository at this point in the history
…` behavior
  • Loading branch information
jeromelaban committed Dec 8, 2022
1 parent c7f011a commit 7aa064d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UI.Dispatching/Core/CoreDispatcher.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Uno.UI.Dispatching
{
internal sealed partial class CoreDispatcher
{
internal bool IsThreadingSupported { get; }
internal static bool IsThreadingSupported { get; }
= Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES")
?.Split(',').Any(v => v.Equals("threads", StringComparison.OrdinalIgnoreCase)) ?? false;

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/DragDrop/DragDropManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void BeginDragAndDrop(CoreDragInfo info, ICoreDropOperationTarget? target
{
if (
#if __WASM__
_window.Dispatcher.IsThreadingSupported &&
Uno.UI.Dispatching.CoreDispatcher.IsThreadingSupported &&
#endif
!_window.Dispatcher.HasThreadAccess)
{
Expand Down
17 changes: 5 additions & 12 deletions src/Uno.UWP/System/DispatcherQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public static DispatcherQueue GetForCurrentThread()
{
if (_current == null) // Do not even check for thread access if we already have a value!
{
#if !__WASM__
// This check is disabled on WASM until threading support is enabled, since HasThreadAccess is currently user-configured (and defaults to false).
if (!CoreDispatcher.Main.HasThreadAccess)
if (
#if __WASM__
CoreDispatcher.IsThreadingSupported &&
#endif
!CoreDispatcher.Main.HasThreadAccess)
{
return default;
}
#endif

_current = new DispatcherQueue();
}
Expand All @@ -42,13 +44,10 @@ public static DispatcherQueue GetForCurrentThread()
/// </summary>
internal static void CheckThreadAccess()
{
#if !__WASM__
// This check is disabled on WASM until threading support is enabled, since HasThreadAccess is currently user-configured (and defaults to false).
if (!CoreDispatcher.Main.HasThreadAccess)
{
throw new InvalidOperationException("The application called an interface that was marshalled for a different thread.");
}
#endif
}

public bool TryEnqueue(DispatcherQueueHandler callback)
Expand All @@ -70,12 +69,6 @@ public bool TryEnqueue(DispatcherQueuePriority priority, DispatcherQueueHandler
}

public bool HasThreadAccess
#if !__WASM__
=> CoreDispatcher.Main.HasThreadAccess;
#else
// This check is disabled on WASM until threading support is enabled, since HasThreadAccess is currently user-configured (and defaults to false).
=> true;
#endif

}
}
2 changes: 0 additions & 2 deletions src/Uno.UWP/UI/Core/CoreDispatcher.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Windows.UI.Core
{
public sealed partial class CoreDispatcher
{
internal bool IsThreadingSupported { get; } = Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_CONFIGURATION").StartsWith("threads", StringComparison.OrdinalIgnoreCase);

/// <summary>
/// Provide a action that will delegate the dispatch of CoreDispatcher work
/// </summary>
Expand Down

0 comments on commit 7aa064d

Please sign in to comment.