44using System ;
55using System . Runtime . ExceptionServices ;
66using System . Threading . Tasks ;
7- using static System . Windows . Threading . Dispatcher ;
7+ using System . Windows ;
8+ using WindowsDispatcher = System . Windows . Threading . Dispatcher ;
89
910namespace Microsoft . AspNetCore . Components . WebView . Wpf
1011{
11- internal class WpfDispatcher : Dispatcher
12+ internal sealed class WpfDispatcher : Dispatcher
1213 {
13- public static Dispatcher Instance { get ; } = new WpfDispatcher ( ) ;
14+ private readonly WindowsDispatcher _windowsDispatcher ;
15+
16+ private WpfDispatcher ( WindowsDispatcher windowsDispatcher )
17+ {
18+ _windowsDispatcher = windowsDispatcher ?? throw new ArgumentNullException ( nameof ( windowsDispatcher ) ) ;
19+ }
20+
21+ public static Dispatcher Instance { get ; } = new WpfDispatcher ( Application . Current . Dispatcher ) ;
1422
1523 private static Action < Exception > RethrowException = exception =>
1624 ExceptionDispatchInfo . Capture ( exception ) . Throw ( ) ;
1725
1826 public override bool CheckAccess ( )
19- => CurrentDispatcher . CheckAccess ( ) ;
27+ => _windowsDispatcher . CheckAccess ( ) ;
2028
2129 public override async Task InvokeAsync ( Action workItem )
2230 {
2331 try
2432 {
25- if ( CurrentDispatcher . CheckAccess ( ) )
33+ if ( _windowsDispatcher . CheckAccess ( ) )
2634 {
2735 workItem ( ) ;
2836 }
2937 else
3038 {
31- await CurrentDispatcher . InvokeAsync ( workItem ) ;
39+ await _windowsDispatcher . InvokeAsync ( workItem ) ;
3240 }
3341 }
3442 catch ( Exception ex )
3543 {
3644 // TODO: Determine whether this is the right kind of rethrowing pattern
3745 // You do have to do something like this otherwise unhandled exceptions
3846 // throw from inside Dispatcher.InvokeAsync are simply lost.
39- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
47+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
4048 throw ;
4149 }
4250 }
@@ -45,21 +53,21 @@ public override async Task InvokeAsync(Func<Task> workItem)
4553 {
4654 try
4755 {
48- if ( CurrentDispatcher . CheckAccess ( ) )
56+ if ( _windowsDispatcher . CheckAccess ( ) )
4957 {
5058 await workItem ( ) ;
5159 }
5260 else
5361 {
54- await CurrentDispatcher . InvokeAsync ( workItem ) ;
62+ await _windowsDispatcher . InvokeAsync ( workItem ) ;
5563 }
5664 }
5765 catch ( Exception ex )
5866 {
5967 // TODO: Determine whether this is the right kind of rethrowing pattern
6068 // You do have to do something like this otherwise unhandled exceptions
6169 // throw from inside Dispatcher.InvokeAsync are simply lost.
62- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
70+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
6371 throw ;
6472 }
6573 }
@@ -68,21 +76,21 @@ public override async Task<TResult> InvokeAsync<TResult>(Func<TResult> workItem)
6876 {
6977 try
7078 {
71- if ( CurrentDispatcher . CheckAccess ( ) )
79+ if ( _windowsDispatcher . CheckAccess ( ) )
7280 {
7381 return workItem ( ) ;
7482 }
7583 else
7684 {
77- return await CurrentDispatcher . InvokeAsync ( workItem ) ;
85+ return await _windowsDispatcher . InvokeAsync ( workItem ) ;
7886 }
7987 }
8088 catch ( Exception ex )
8189 {
8290 // TODO: Determine whether this is the right kind of rethrowing pattern
8391 // You do have to do something like this otherwise unhandled exceptions
8492 // throw from inside Dispatcher.InvokeAsync are simply lost.
85- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
93+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
8694 throw ;
8795 }
8896 }
@@ -91,21 +99,21 @@ public override async Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> wor
9199 {
92100 try
93101 {
94- if ( CurrentDispatcher . CheckAccess ( ) )
102+ if ( _windowsDispatcher . CheckAccess ( ) )
95103 {
96104 return await workItem ( ) ;
97105 }
98106 else
99107 {
100- return await CurrentDispatcher . InvokeAsync ( workItem ) . Task . Unwrap ( ) ;
108+ return await _windowsDispatcher . InvokeAsync ( workItem ) . Task . Unwrap ( ) ;
101109 }
102110 }
103111 catch ( Exception ex )
104112 {
105113 // TODO: Determine whether this is the right kind of rethrowing pattern
106114 // You do have to do something like this otherwise unhandled exceptions
107115 // throw from inside Dispatcher.InvokeAsync are simply lost.
108- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
116+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
109117 throw ;
110118 }
111119 }
0 commit comments