Skip to content

Commit

Permalink
For Blazor.WebView.Wpf, use application dispatcher (#32180)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS authored Apr 27, 2021
1 parent 3c91c4a commit 1453c5b
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/Components/WebView/Platforms/Wpf/src/WpfDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,47 @@
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
internal sealed 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<Exception> 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)
{
// 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;
}
}
Expand All @@ -45,21 +53,21 @@ public override async Task InvokeAsync(Func<Task> workItem)
{
try
{
if (CurrentDispatcher.CheckAccess())
if (_windowsDispatcher.CheckAccess())
{
await workItem();
}
else
{
await CurrentDispatcher.InvokeAsync(workItem);
await _windowsDispatcher.InvokeAsync(workItem);
}
}
catch (Exception ex)
{
// 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;
}
}
Expand All @@ -68,21 +76,21 @@ public override async Task<TResult> InvokeAsync<TResult>(Func<TResult> workItem)
{
try
{
if (CurrentDispatcher.CheckAccess())
if (_windowsDispatcher.CheckAccess())
{
return workItem();
}
else
{
return await CurrentDispatcher.InvokeAsync(workItem);
return await _windowsDispatcher.InvokeAsync(workItem);
}
}
catch (Exception ex)
{
// 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;
}
}
Expand All @@ -91,21 +99,21 @@ public override async Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> 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)
{
// 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;
}
}
Expand Down

0 comments on commit 1453c5b

Please sign in to comment.