From 4c2baffb503b8e0312fafeebace18e64c0fcd99f Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Mon, 26 Apr 2021 16:56:35 +0100 Subject: [PATCH 1/2] For Blazor.WebView.Wpf, use application dispatcher --- .../Platforms/Wpf/src/WpfDispatcher.cs | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs b/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs index f71c8b6f204c..4ce70abd6214 100644 --- a/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs +++ b/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs @@ -4,31 +4,39 @@ using System; using System.Runtime.ExceptionServices; using System.Threading.Tasks; -using static System.Windows.Threading.Dispatcher; +using System.Windows; +using WindowsDispatcher = System.Windows.Threading.Dispatcher; namespace Microsoft.AspNetCore.Components.WebView.Wpf { internal class WpfDispatcher : Dispatcher { - public static Dispatcher Instance { get; } = new WpfDispatcher(); + private readonly WindowsDispatcher _windowsDispatcher; + + private WpfDispatcher(WindowsDispatcher windowsDispatcher) + { + _windowsDispatcher = windowsDispatcher ?? throw new ArgumentNullException(nameof(windowsDispatcher)); + } + + public static Dispatcher Instance { get; } = new WpfDispatcher(Application.Current.Dispatcher); private static Action RethrowException = exception => ExceptionDispatchInfo.Capture(exception).Throw(); public override bool CheckAccess() - => CurrentDispatcher.CheckAccess(); + => _windowsDispatcher.CheckAccess(); public override async Task InvokeAsync(Action workItem) { try { - if (CurrentDispatcher.CheckAccess()) + if (_windowsDispatcher.CheckAccess()) { workItem(); } else { - await CurrentDispatcher.InvokeAsync(workItem); + await _windowsDispatcher.InvokeAsync(workItem); } } catch (Exception ex) @@ -36,7 +44,7 @@ public override async Task InvokeAsync(Action workItem) // TODO: Determine whether this is the right kind of rethrowing pattern // You do have to do something like this otherwise unhandled exceptions // throw from inside Dispatcher.InvokeAsync are simply lost. - _ = CurrentDispatcher.BeginInvoke(RethrowException, ex); + _ = _windowsDispatcher.BeginInvoke(RethrowException, ex); throw; } } @@ -45,13 +53,13 @@ public override async Task InvokeAsync(Func workItem) { try { - if (CurrentDispatcher.CheckAccess()) + if (_windowsDispatcher.CheckAccess()) { await workItem(); } else { - await CurrentDispatcher.InvokeAsync(workItem); + await _windowsDispatcher.InvokeAsync(workItem); } } catch (Exception ex) @@ -59,7 +67,7 @@ public override async Task InvokeAsync(Func workItem) // TODO: Determine whether this is the right kind of rethrowing pattern // You do have to do something like this otherwise unhandled exceptions // throw from inside Dispatcher.InvokeAsync are simply lost. - _ = CurrentDispatcher.BeginInvoke(RethrowException, ex); + _ = _windowsDispatcher.BeginInvoke(RethrowException, ex); throw; } } @@ -68,13 +76,13 @@ public override async Task InvokeAsync(Func workItem) { try { - if (CurrentDispatcher.CheckAccess()) + if (_windowsDispatcher.CheckAccess()) { return workItem(); } else { - return await CurrentDispatcher.InvokeAsync(workItem); + return await _windowsDispatcher.InvokeAsync(workItem); } } catch (Exception ex) @@ -82,7 +90,7 @@ public override async Task InvokeAsync(Func workItem) // TODO: Determine whether this is the right kind of rethrowing pattern // You do have to do something like this otherwise unhandled exceptions // throw from inside Dispatcher.InvokeAsync are simply lost. - _ = CurrentDispatcher.BeginInvoke(RethrowException, ex); + _ = _windowsDispatcher.BeginInvoke(RethrowException, ex); throw; } } @@ -91,13 +99,13 @@ public override async Task InvokeAsync(Func> wor { try { - if (CurrentDispatcher.CheckAccess()) + if (_windowsDispatcher.CheckAccess()) { return await workItem(); } else { - return await CurrentDispatcher.InvokeAsync(workItem).Task.Unwrap(); + return await _windowsDispatcher.InvokeAsync(workItem).Task.Unwrap(); } } catch (Exception ex) @@ -105,7 +113,7 @@ public override async Task InvokeAsync(Func> wor // TODO: Determine whether this is the right kind of rethrowing pattern // You do have to do something like this otherwise unhandled exceptions // throw from inside Dispatcher.InvokeAsync are simply lost. - _ = CurrentDispatcher.BeginInvoke(RethrowException, ex); + _ = _windowsDispatcher.BeginInvoke(RethrowException, ex); throw; } } From 485839071fc8227dea31f629e8569bbec992320a Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Tue, 27 Apr 2021 10:28:22 +0100 Subject: [PATCH 2/2] Update src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs Co-authored-by: Pranav K --- src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs b/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs index 4ce70abd6214..24fa3cb54a71 100644 --- a/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs +++ b/src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Components.WebView.Wpf { - internal class WpfDispatcher : Dispatcher + internal sealed class WpfDispatcher : Dispatcher { private readonly WindowsDispatcher _windowsDispatcher;