From 7aa064d0e366ed0b5b87e56f59372595fa08b35d Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Thu, 8 Dec 2022 08:55:51 -0500 Subject: [PATCH] fix(dispatcherqueue): Use proper `CheckThreadAccess`/`HasThreadAccess` behavior --- .../Core/CoreDispatcher.wasm.cs | 2 +- src/Uno.UI/UI/Xaml/DragDrop/DragDropManager.cs | 2 +- src/Uno.UWP/System/DispatcherQueue.cs | 17 +++++------------ src/Uno.UWP/UI/Core/CoreDispatcher.wasm.cs | 2 -- 4 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Uno.UI.Dispatching/Core/CoreDispatcher.wasm.cs b/src/Uno.UI.Dispatching/Core/CoreDispatcher.wasm.cs index 3f64457110d9..3803f4fba383 100644 --- a/src/Uno.UI.Dispatching/Core/CoreDispatcher.wasm.cs +++ b/src/Uno.UI.Dispatching/Core/CoreDispatcher.wasm.cs @@ -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; diff --git a/src/Uno.UI/UI/Xaml/DragDrop/DragDropManager.cs b/src/Uno.UI/UI/Xaml/DragDrop/DragDropManager.cs index f43be0a077fb..d89cf4b9a924 100644 --- a/src/Uno.UI/UI/Xaml/DragDrop/DragDropManager.cs +++ b/src/Uno.UI/UI/Xaml/DragDrop/DragDropManager.cs @@ -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) { diff --git a/src/Uno.UWP/System/DispatcherQueue.cs b/src/Uno.UWP/System/DispatcherQueue.cs index 9f45322cd4ef..bbc4205b35cc 100644 --- a/src/Uno.UWP/System/DispatcherQueue.cs +++ b/src/Uno.UWP/System/DispatcherQueue.cs @@ -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(); } @@ -42,13 +44,10 @@ public static DispatcherQueue GetForCurrentThread() /// 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) @@ -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 - } } diff --git a/src/Uno.UWP/UI/Core/CoreDispatcher.wasm.cs b/src/Uno.UWP/UI/Core/CoreDispatcher.wasm.cs index 836d8b103924..f4f2106346b8 100644 --- a/src/Uno.UWP/UI/Core/CoreDispatcher.wasm.cs +++ b/src/Uno.UWP/UI/Core/CoreDispatcher.wasm.cs @@ -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); - /// /// Provide a action that will delegate the dispatch of CoreDispatcher work ///