-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
DispatcherHelper.cs
373 lines (334 loc) · 19.4 KB
/
DispatcherHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
namespace Microsoft.Toolkit.Uwp.Helpers
{
/// <summary>
/// This class provides static methods helper for executing code in UI thread of the main window.
/// </summary>
[Obsolete("Replace calls to APIs in this class with extensions for the Windows.System.DispatcherQueue type (see https://docs.microsoft.com/uwp/api/windows.system.dispatcherqueue).")]
public static class DispatcherHelper
{
/// <summary>
/// Executes the given function on the main view's UI thread.
/// </summary>
/// <param name="function">Synchronous function to be executed on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")]
public static Task ExecuteOnUIThreadAsync(Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}
/// <summary>
/// Executes the given function on the main view's UI thread and returns its result.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="function">Synchronous function to be executed on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task{T}"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")]
public static Task<T> ExecuteOnUIThreadAsync<T>(Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}
/// <summary>
/// Executes the given <see cref="Task"/>-returning function on the main view's UI thread and returns either that <see cref="Task"/>
/// or a proxy <see cref="Task"/> that completes when the one produced by the given function completes.
/// </summary>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")]
public static Task ExecuteOnUIThreadAsync(Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}
/// <summary>
/// Executes the given <see cref="Task{TResult}"/>-returning function on the main view's UI thread and returns either that <see cref="Task{TResult}"/>
/// or a proxy <see cref="Task{TResult}"/> that completes when the one produced by the given function completes.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task{T}"/> for the operation.</returns>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority), where dispatcherQueue is a DispatcherQueue instance that was retrieved from the UI thread and stored for later use.")]
public static Task<T> ExecuteOnUIThreadAsync<T>(Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
return ExecuteOnUIThreadAsync(CoreApplication.MainView, function, priority);
}
/// <summary>
/// Executes the given function on a given view's UI thread.
/// </summary>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on.</param>
/// <param name="function">Synchronous function to be executed on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")]
public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (viewToExecuteOn is null)
{
throw new ArgumentNullException(nameof(viewToExecuteOn));
}
return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority);
}
/// <summary>
/// Executes the given function on a given view's UI thread.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on.</param>
/// <param name="function">Synchronous function with return type <typeparamref name="T"/> to be executed on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task{T}"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")]
public static Task<T> ExecuteOnUIThreadAsync<T>(this CoreApplicationView viewToExecuteOn, Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (viewToExecuteOn is null)
{
throw new ArgumentNullException(nameof(viewToExecuteOn));
}
return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority);
}
/// <summary>
/// Executes the given function on a given view's UI thread.
/// </summary>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on.</param>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")]
public static Task ExecuteOnUIThreadAsync(this CoreApplicationView viewToExecuteOn, Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (viewToExecuteOn is null)
{
throw new ArgumentNullException(nameof(viewToExecuteOn));
}
return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority);
}
/// <summary>
/// Executes the given function on a given view's UI thread.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="viewToExecuteOn">View for the <paramref name="function"/> to be executed on.</param>
/// <param name="function">Asynchronous function to be executed asynchronously on UI thread.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with viewToExecuteOn.DispatcherQueue.EnqueueAsync(function, priority).")]
public static Task<T> ExecuteOnUIThreadAsync<T>(this CoreApplicationView viewToExecuteOn, Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (viewToExecuteOn is null)
{
throw new ArgumentNullException(nameof(viewToExecuteOn));
}
return viewToExecuteOn.Dispatcher.AwaitableRunAsync(function, priority);
}
/// <summary>
/// Extension method for <see cref="CoreDispatcher"/>. Offering an actual awaitable <see cref="Task"/> with optional result that will be executed on the given dispatcher.
/// </summary>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/>.</param>
/// <param name="function"> Function to be executed on the given dispatcher.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")]
public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (function is null)
{
throw new ArgumentNullException(nameof(function));
}
/* Run the function directly when we have thread access.
* Also reuse Task.CompletedTask in case of success,
* to skip an unnecessary heap allocation for every invocation. */
if (dispatcher.HasThreadAccess)
{
try
{
function();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}
var taskCompletionSource = new TaskCompletionSource<object>();
_ = dispatcher.RunAsync(priority, () =>
{
try
{
function();
taskCompletionSource.SetResult(null);
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
});
return taskCompletionSource.Task;
}
/// <summary>
/// Extension method for <see cref="CoreDispatcher"/>. Offering an actual awaitable <see cref="Task{T}"/> with optional result that will be executed on the given dispatcher.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/>.</param>
/// <param name="function"> Function to be executed on the given dispatcher.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task{T}"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")]
public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<T> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (function is null)
{
throw new ArgumentNullException(nameof(function));
}
// Skip the dispatch, if possible
if (dispatcher.HasThreadAccess)
{
try
{
return Task.FromResult(function());
}
catch (Exception e)
{
return Task.FromException<T>(e);
}
}
var taskCompletionSource = new TaskCompletionSource<T>();
_ = dispatcher.RunAsync(priority, () =>
{
try
{
taskCompletionSource.SetResult(function());
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
});
return taskCompletionSource.Task;
}
/// <summary>
/// Extension method for <see cref="CoreDispatcher"/>. Offering an actual awaitable <see cref="Task"/> with optional result that will be executed on the given dispatcher.
/// </summary>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/>.</param>
/// <param name="function">Asynchronous function to be executed on the given dispatcher.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")]
public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Func<Task> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (function is null)
{
throw new ArgumentNullException(nameof(function));
}
/* If we have thread access, we can retrieve the task directly.
* We don't use ConfigureAwait(false) in this case, in order
* to let the caller continue its execution on the same thread
* after awaiting the task returned by this function. */
if (dispatcher.HasThreadAccess)
{
try
{
if (function() is Task awaitableResult)
{
return awaitableResult;
}
return Task.FromException(new InvalidOperationException("The Task returned by function cannot be null."));
}
catch (Exception e)
{
return Task.FromException(e);
}
}
var taskCompletionSource = new TaskCompletionSource<object>();
_ = dispatcher.RunAsync(priority, async () =>
{
try
{
if (function() is Task awaitableResult)
{
await awaitableResult.ConfigureAwait(false);
taskCompletionSource.SetResult(null);
}
else
{
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
}
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
});
return taskCompletionSource.Task;
}
/// <summary>
/// Extension method for <see cref="CoreDispatcher"/>. Offering an actual awaitable <see cref="Task{T}"/> with optional result that will be executed on the given dispatcher.
/// </summary>
/// <typeparam name="T">Returned data type of the function.</typeparam>
/// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/>.</param>
/// <param name="function">Asynchronous function to be executed asynchronously on the given dispatcher.</param>
/// <param name="priority">Dispatcher execution priority, default is normal.</param>
/// <returns>An awaitable <see cref="Task{T}"/> for the operation.</returns>
/// <remarks>If the current thread has UI access, <paramref name="function"/> will be invoked directly.</remarks>
[Obsolete("This method should be replaced with dispatcherQueue.EnqueueAsync(function, priority). A queue can be retrieved with DispatcherQueue.GetForCurrentThread().")]
public static Task<T> AwaitableRunAsync<T>(this CoreDispatcher dispatcher, Func<Task<T>> function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
if (function is null)
{
throw new ArgumentNullException(nameof(function));
}
// Skip the dispatch, if possible
if (dispatcher.HasThreadAccess)
{
try
{
if (function() is Task<T> awaitableResult)
{
return awaitableResult;
}
return Task.FromException<T>(new InvalidOperationException("The Task returned by function cannot be null."));
}
catch (Exception e)
{
return Task.FromException<T>(e);
}
}
var taskCompletionSource = new TaskCompletionSource<T>();
_ = dispatcher.RunAsync(priority, async () =>
{
try
{
if (function() is Task<T> awaitableResult)
{
var result = await awaitableResult.ConfigureAwait(false);
taskCompletionSource.SetResult(result);
}
else
{
taskCompletionSource.SetException(new InvalidOperationException("The Task returned by function cannot be null."));
}
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
});
return taskCompletionSource.Task;
}
}
}