@@ -44,31 +44,32 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Action function
44
44
}
45
45
}
46
46
47
- static Task TryEnqueueAsync ( DispatcherQueue dispatcher , Action function , DispatcherQueuePriority priority )
48
- {
49
- var taskCompletionSource = new TaskCompletionSource < object ? > ( ) ;
47
+ var taskCompletionSource = new TaskCompletionSource ( ) ;
50
48
51
- if ( ! dispatcher . TryEnqueue ( priority , ( ) =>
49
+ bool success = dispatcher . TryEnqueue (
50
+ priority ,
51
+ static ( function , taskCompletionSource ) =>
52
52
{
53
53
try
54
54
{
55
55
function ( ) ;
56
56
57
- taskCompletionSource . SetResult ( null ) ;
57
+ taskCompletionSource . SetResult ( ) ;
58
58
}
59
59
catch ( Exception e )
60
60
{
61
61
taskCompletionSource . SetException ( e ) ;
62
62
}
63
- } ) )
64
- {
65
- taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
66
- }
63
+ } ,
64
+ function ,
65
+ taskCompletionSource ) ;
67
66
68
- return taskCompletionSource . Task ;
67
+ if ( success )
68
+ {
69
+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
69
70
}
70
71
71
- return TryEnqueueAsync ( dispatcher , function , priority ) ;
72
+ return taskCompletionSource . Task ;
72
73
}
73
74
74
75
/// <summary>
@@ -95,11 +96,11 @@ public static Task<T> EnqueueAsync<T>(this DispatcherQueue dispatcher, Func<T> f
95
96
}
96
97
}
97
98
98
- static Task < T > TryEnqueueAsync ( DispatcherQueue dispatcher , Func < T > function , DispatcherQueuePriority priority )
99
- {
100
- var taskCompletionSource = new TaskCompletionSource < T > ( ) ;
99
+ var taskCompletionSource = new TaskCompletionSource < T > ( ) ;
101
100
102
- if ( ! dispatcher . TryEnqueue ( priority , ( ) =>
101
+ bool success = dispatcher . TryEnqueue (
102
+ priority ,
103
+ static ( function , taskCompletionSource ) =>
103
104
{
104
105
try
105
106
{
@@ -109,15 +110,16 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<T> function, Dis
109
110
{
110
111
taskCompletionSource . SetException ( e ) ;
111
112
}
112
- } ) )
113
- {
114
- taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
115
- }
113
+ } ,
114
+ function ,
115
+ taskCompletionSource ) ;
116
116
117
- return taskCompletionSource . Task ;
117
+ if ( ! success )
118
+ {
119
+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
118
120
}
119
121
120
- return TryEnqueueAsync ( dispatcher , function , priority ) ;
122
+ return taskCompletionSource . Task ;
121
123
}
122
124
123
125
/// <summary>
@@ -152,19 +154,19 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func<Task> func
152
154
}
153
155
}
154
156
155
- static Task TryEnqueueAsync ( DispatcherQueue dispatcher , Func < Task > function , DispatcherQueuePriority priority )
156
- {
157
- var taskCompletionSource = new TaskCompletionSource < object ? > ( ) ;
157
+ var taskCompletionSource = new TaskCompletionSource ( ) ;
158
158
159
- if ( ! dispatcher . TryEnqueue ( priority , async ( ) =>
159
+ bool success = dispatcher . TryEnqueue (
160
+ priority ,
161
+ static async ( function , taskCompletionSource ) =>
160
162
{
161
163
try
162
164
{
163
165
if ( function ( ) is Task awaitableResult )
164
166
{
165
167
await awaitableResult . ConfigureAwait ( false ) ;
166
168
167
- taskCompletionSource . SetResult ( null ) ;
169
+ taskCompletionSource . SetResult ( ) ;
168
170
}
169
171
else
170
172
{
@@ -175,15 +177,16 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task> function, Dis
175
177
{
176
178
taskCompletionSource . SetException ( e ) ;
177
179
}
178
- } ) )
179
- {
180
- taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
181
- }
180
+ } ,
181
+ function ,
182
+ taskCompletionSource ) ;
182
183
183
- return taskCompletionSource . Task ;
184
+ if ( ! success )
185
+ {
186
+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
184
187
}
185
188
186
- return TryEnqueueAsync ( dispatcher , function , priority ) ;
189
+ return taskCompletionSource . Task ;
187
190
}
188
191
189
192
/// <summary>
@@ -215,11 +218,11 @@ public static Task<T> EnqueueAsync<T>(this DispatcherQueue dispatcher, Func<Task
215
218
}
216
219
}
217
220
218
- static Task < T > TryEnqueueAsync ( DispatcherQueue dispatcher , Func < Task < T > > function , DispatcherQueuePriority priority )
219
- {
220
- var taskCompletionSource = new TaskCompletionSource < T > ( ) ;
221
+ var taskCompletionSource = new TaskCompletionSource < T > ( ) ;
221
222
222
- if ( ! dispatcher . TryEnqueue ( priority , async ( ) =>
223
+ bool success = dispatcher . TryEnqueue (
224
+ priority ,
225
+ static async ( function , taskCompletionSource ) =>
223
226
{
224
227
try
225
228
{
@@ -238,15 +241,16 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task<T>> functio
238
241
{
239
242
taskCompletionSource . SetException ( e ) ;
240
243
}
241
- } ) )
242
- {
243
- taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
244
- }
244
+ } ,
245
+ function ,
246
+ taskCompletionSource ) ;
245
247
246
- return taskCompletionSource . Task ;
248
+ if ( ! success )
249
+ {
250
+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
247
251
}
248
252
249
- return TryEnqueueAsync ( dispatcher , function , priority ) ;
253
+ return taskCompletionSource . Task ;
250
254
}
251
255
252
256
/// <summary>
0 commit comments