4
4
using System ;
5
5
using System . Runtime . ExceptionServices ;
6
6
using System . Threading . Tasks ;
7
- using static System . Windows . Threading . Dispatcher ;
7
+ using System . Windows ;
8
+ using WindowsDispatcher = System . Windows . Threading . Dispatcher ;
8
9
9
10
namespace Microsoft . AspNetCore . Components . WebView . Wpf
10
11
{
11
- internal class WpfDispatcher : Dispatcher
12
+ internal sealed class WpfDispatcher : Dispatcher
12
13
{
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 ) ;
14
22
15
23
private static Action < Exception > RethrowException = exception =>
16
24
ExceptionDispatchInfo . Capture ( exception ) . Throw ( ) ;
17
25
18
26
public override bool CheckAccess ( )
19
- => CurrentDispatcher . CheckAccess ( ) ;
27
+ => _windowsDispatcher . CheckAccess ( ) ;
20
28
21
29
public override async Task InvokeAsync ( Action workItem )
22
30
{
23
31
try
24
32
{
25
- if ( CurrentDispatcher . CheckAccess ( ) )
33
+ if ( _windowsDispatcher . CheckAccess ( ) )
26
34
{
27
35
workItem ( ) ;
28
36
}
29
37
else
30
38
{
31
- await CurrentDispatcher . InvokeAsync ( workItem ) ;
39
+ await _windowsDispatcher . InvokeAsync ( workItem ) ;
32
40
}
33
41
}
34
42
catch ( Exception ex )
35
43
{
36
44
// TODO: Determine whether this is the right kind of rethrowing pattern
37
45
// You do have to do something like this otherwise unhandled exceptions
38
46
// throw from inside Dispatcher.InvokeAsync are simply lost.
39
- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
47
+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
40
48
throw ;
41
49
}
42
50
}
@@ -45,21 +53,21 @@ public override async Task InvokeAsync(Func<Task> workItem)
45
53
{
46
54
try
47
55
{
48
- if ( CurrentDispatcher . CheckAccess ( ) )
56
+ if ( _windowsDispatcher . CheckAccess ( ) )
49
57
{
50
58
await workItem ( ) ;
51
59
}
52
60
else
53
61
{
54
- await CurrentDispatcher . InvokeAsync ( workItem ) ;
62
+ await _windowsDispatcher . InvokeAsync ( workItem ) ;
55
63
}
56
64
}
57
65
catch ( Exception ex )
58
66
{
59
67
// TODO: Determine whether this is the right kind of rethrowing pattern
60
68
// You do have to do something like this otherwise unhandled exceptions
61
69
// throw from inside Dispatcher.InvokeAsync are simply lost.
62
- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
70
+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
63
71
throw ;
64
72
}
65
73
}
@@ -68,21 +76,21 @@ public override async Task<TResult> InvokeAsync<TResult>(Func<TResult> workItem)
68
76
{
69
77
try
70
78
{
71
- if ( CurrentDispatcher . CheckAccess ( ) )
79
+ if ( _windowsDispatcher . CheckAccess ( ) )
72
80
{
73
81
return workItem ( ) ;
74
82
}
75
83
else
76
84
{
77
- return await CurrentDispatcher . InvokeAsync ( workItem ) ;
85
+ return await _windowsDispatcher . InvokeAsync ( workItem ) ;
78
86
}
79
87
}
80
88
catch ( Exception ex )
81
89
{
82
90
// TODO: Determine whether this is the right kind of rethrowing pattern
83
91
// You do have to do something like this otherwise unhandled exceptions
84
92
// throw from inside Dispatcher.InvokeAsync are simply lost.
85
- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
93
+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
86
94
throw ;
87
95
}
88
96
}
@@ -91,21 +99,21 @@ public override async Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> wor
91
99
{
92
100
try
93
101
{
94
- if ( CurrentDispatcher . CheckAccess ( ) )
102
+ if ( _windowsDispatcher . CheckAccess ( ) )
95
103
{
96
104
return await workItem ( ) ;
97
105
}
98
106
else
99
107
{
100
- return await CurrentDispatcher . InvokeAsync ( workItem ) . Task . Unwrap ( ) ;
108
+ return await _windowsDispatcher . InvokeAsync ( workItem ) . Task . Unwrap ( ) ;
101
109
}
102
110
}
103
111
catch ( Exception ex )
104
112
{
105
113
// TODO: Determine whether this is the right kind of rethrowing pattern
106
114
// You do have to do something like this otherwise unhandled exceptions
107
115
// throw from inside Dispatcher.InvokeAsync are simply lost.
108
- _ = CurrentDispatcher . BeginInvoke ( RethrowException , ex ) ;
116
+ _ = _windowsDispatcher . BeginInvoke ( RethrowException , ex ) ;
109
117
throw ;
110
118
}
111
119
}
0 commit comments