This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
forked from dotnet/dotNext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AsyncDelegate.cs
497 lines (451 loc) · 27.8 KB
/
AsyncDelegate.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
namespace DotNext.Threading
{
using static Tasks.Continuation;
/// <summary>
/// Provides set of methods for asynchronous invocation of various delegates.
/// </summary>
/// <remarks>
/// BeginInvoke and EndInvoke methods of delegate type are supported only in .NET Framework.
/// This class provides alternative approach which allows to invoke delegate asynchronously
/// with full support of async/await feature.
/// </remarks>
/// <seealso href="https://github.com/dotnet/corefx/issues/5940">BeginInvoke throws NotSupportedException</seealso>
public static class AsyncDelegate
{
private sealed unsafe class InvocationWorkItem<TDelegate, TContext>
where TDelegate : MulticastDelegate
{
private readonly CancellationToken token;
private readonly TDelegate invocationList;
private readonly TContext context;
private readonly delegate*<TDelegate, in TContext, void> invoker;
internal InvocationWorkItem(TDelegate invocationList, delegate*<TDelegate, in TContext, void> invoker, in TContext context, CancellationToken token)
{
this.token = token;
this.invocationList = invocationList;
this.invoker = invoker;
this.context = context;
}
internal void Invoke()
{
// null when no errors
// Exception when single error
// ICollection<Exception> when multiple errors
object? errors = null;
foreach (TDelegate target in invocationList.GetInvocationList())
{
if (token.IsCancellationRequested)
{
AddError(ref errors, new OperationCanceledException(token));
break;
}
try
{
invoker(target, in context);
}
catch (Exception e)
{
AddError(ref errors, e);
}
}
// aggregate all exceptions
switch (errors)
{
case Exception e:
ExceptionDispatchInfo.Throw(e);
break;
case IEnumerable<Exception> e:
throw new AggregateException(e);
}
static void AddError(ref object? errors, Exception e)
{
switch (errors)
{
case null:
errors = e;
break;
case Exception error:
errors = CreateList(error, e);
break;
case ICollection<Exception> collection:
collection.Add(e);
break;
}
}
static ICollection<Exception> CreateList(Exception first, Exception second)
{
ICollection<Exception> result = new LinkedList<Exception>();
result.Add(first);
result.Add(second);
return result;
}
}
}
private static unsafe Task InvokeAsync<TDelegate, TContext>(TDelegate @delegate, in TContext context, delegate*<TDelegate, in TContext, void> invoker, CancellationToken token)
where TDelegate : MulticastDelegate
{
if (token.IsCancellationRequested)
return Task.FromCanceled(token);
return Task.Factory.StartNew(new InvocationWorkItem<TDelegate, TContext>(@delegate, invoker, context, token).Invoke, token, TaskCreationOptions.None, TaskScheduler.Default);
}
/// <summary>
/// Invokes a delegate of arbitrary type asynchronously.
/// </summary>
/// <param name="delegate">A delegate to be invoked asynchronously.</param>
/// <param name="invoker">Synchronous invoker of the delegate from invocation list.</param>
/// <param name="token">Cancellation token.</param>
/// <typeparam name="TDelegate">Type of delegate to invoke.</typeparam>
/// <returns>A task allows to control asynchronous invocation of methods attached to the multicast delegate.</returns>
public static unsafe Task InvokeAsync<TDelegate>(this TDelegate @delegate, Action<TDelegate> invoker, CancellationToken token = default)
where TDelegate : MulticastDelegate
{
return InvokeAsync(@delegate, invoker, &Invoke, token);
static void Invoke(TDelegate target, in Action<TDelegate> invoker) => invoker(target);
}
/// <summary>
/// Invokes event handlers asynchronously.
/// </summary>
/// <typeparam name="TEventArgs">Type of event object.</typeparam>
/// <param name="handler">A set event handlers combined as single delegate.</param>
/// <param name="sender">Event sender.</param>
/// <param name="args">Event arguments.</param>
/// <param name="token">Optional cancellation token.</param>
/// <returns>An object representing state of the asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<TEventArgs>(this EventHandler<TEventArgs> handler, object sender, TEventArgs args, CancellationToken token = default)
{
return InvokeAsync(handler, (sender, args), &Invoke, token);
static void Invoke(EventHandler<TEventArgs> handler, in (object, TEventArgs) args)
=> handler(args.Item1, args.Item2);
}
/// <summary>
/// Invokes event handlers asynchronously.
/// </summary>
/// <param name="handler">A set event handlers combined as single delegate.</param>
/// <param name="sender">Event sender.</param>
/// <param name="args">Event arguments.</param>
/// <param name="token">Optional cancellation token.</param>
/// <returns>An object representing state of the asynchronous invocation.</returns>
public static unsafe Task InvokeAsync(this EventHandler handler, object sender, EventArgs args, CancellationToken token = default)
{
return InvokeAsync(handler, (sender, args), &Invoke, token);
static void Invoke(EventHandler handler, in (object, EventArgs) args)
=> handler(args.Item1, args.Item2);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync(this Action action, CancellationToken token = default)
{
return InvokeAsync(action, Missing.Value, &Invoke, token);
static void Invoke(Action handler, in Missing args)
=> handler();
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T">Type of the action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg">The action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T>(this Action<T> action, T arg, CancellationToken token = default)
{
return InvokeAsync(action, arg, &Invoke, token);
static void Invoke(Action<T> handler, in T arg)
=> handler(arg);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2>(this Action<T1, T2> action, T1 arg1, T2 arg2, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2), &Invoke, token);
static void Invoke(Action<T1, T2> action, in (T1, T2) args)
=> action(args.Item1, args.Item2);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3>(this Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3), &Invoke, token);
static void Invoke(Action<T1, T2, T3> action, in (T1, T2, T3) args)
=> action(args.Item1, args.Item2, args.Item3);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <typeparam name="T4">Type of the fourth action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="arg4">The fourth action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3, T4>(this Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3, arg4), &Invoke, token);
static void Invoke(Action<T1, T2, T3, T4> action, in (T1, T2, T3, T4) args)
=> action(args.Item1, args.Item2, args.Item3, args.Item4);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <typeparam name="T4">Type of the fourth action argument.</typeparam>
/// <typeparam name="T5">Type of the fifth action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="arg4">The fourth action argument.</param>
/// <param name="arg5">The fifth action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3, T4, T5>(this Action<T1, T2, T3, T4, T5> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3, arg4, arg5), &Invoke, token);
static void Invoke(Action<T1, T2, T3, T4, T5> action, in (T1, T2, T3, T4, T5) args)
=> action(args.Item1, args.Item2, args.Item3, args.Item4, args.Item5);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <typeparam name="T4">Type of the fourth action argument.</typeparam>
/// <typeparam name="T5">Type of the fifth action argument.</typeparam>
/// <typeparam name="T6">Type of the sixth action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="arg4">The fourth action argument.</param>
/// <param name="arg5">The fifth action argument.</param>
/// <param name="arg6">The sixth action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3, T4, T5, T6>(this Action<T1, T2, T3, T4, T5, T6> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3, arg4, arg5, arg6), &Invoke, token);
static void Invoke(Action<T1, T2, T3, T4, T5, T6> action, in (T1, T2, T3, T4, T5, T6) args)
=> action(args.Item1, args.Item2, args.Item3, args.Item4, args.Item5, args.Item6);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <typeparam name="T4">Type of the fourth action argument.</typeparam>
/// <typeparam name="T5">Type of the fifth action argument.</typeparam>
/// <typeparam name="T6">Type of the sixth action argument.</typeparam>
/// <typeparam name="T7">Type of the seventh action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="arg4">The fourth action argument.</param>
/// <param name="arg5">The fifth action argument.</param>
/// <param name="arg6">The sixth action argument.</param>
/// <param name="arg7">The seventh action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7>(this Action<T1, T2, T3, T4, T5, T6, T7> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3, arg4, arg5, arg6, arg7), &Invoke, token);
static void Invoke(Action<T1, T2, T3, T4, T5, T6, T7> action, in (T1, T2, T3, T4, T5, T6, T7) args)
=> action(args.Item1, args.Item2, args.Item3, args.Item4, args.Item5, args.Item6, args.Item7);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <typeparam name="T4">Type of the fourth action argument.</typeparam>
/// <typeparam name="T5">Type of the fifth action argument.</typeparam>
/// <typeparam name="T6">Type of the sixth action argument.</typeparam>
/// <typeparam name="T7">Type of the seventh action argument.</typeparam>
/// <typeparam name="T8">Type of the eighth action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="arg4">The fourth action argument.</param>
/// <param name="arg5">The fifth action argument.</param>
/// <param name="arg6">The sixth action argument.</param>
/// <param name="arg7">The seventh action argument.</param>
/// <param name="arg8">The eighth action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8>(this Action<T1, T2, T3, T4, T5, T6, T7, T8> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8), &Invoke, token);
static void Invoke(Action<T1, T2, T3, T4, T5, T6, T7, T8> action, in (T1, T2, T3, T4, T5, T6, T7, T8) args)
=> action(args.Item1, args.Item2, args.Item3, args.Item4, args.Item5, args.Item6, args.Item7, args.Item8);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <typeparam name="T4">Type of the fourth action argument.</typeparam>
/// <typeparam name="T5">Type of the fifth action argument.</typeparam>
/// <typeparam name="T6">Type of the sixth action argument.</typeparam>
/// <typeparam name="T7">Type of the seventh action argument.</typeparam>
/// <typeparam name="T8">Type of the eighth action argument.</typeparam>
/// <typeparam name="T9">Type of the ninth action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="arg4">The fourth action argument.</param>
/// <param name="arg5">The fifth action argument.</param>
/// <param name="arg6">The sixth action argument.</param>
/// <param name="arg7">The seventh action argument.</param>
/// <param name="arg8">The eighth action argument.</param>
/// <param name="arg9">THe ninth action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9>(this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9), &Invoke, token);
static void Invoke(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action, in (T1, T2, T3, T4, T5, T6, T7, T8, T9) args)
=> action(args.Item1, args.Item2, args.Item3, args.Item4, args.Item5, args.Item6, args.Item7, args.Item8, args.Item9);
}
/// <summary>
/// Invokes action asynchronously.
/// </summary>
/// <typeparam name="T1">Type of the first action argument.</typeparam>
/// <typeparam name="T2">Type of the second action argument.</typeparam>
/// <typeparam name="T3">Type of the third action argument.</typeparam>
/// <typeparam name="T4">Type of the fourth action argument.</typeparam>
/// <typeparam name="T5">Type of the fifth action argument.</typeparam>
/// <typeparam name="T6">Type of the sixth action argument.</typeparam>
/// <typeparam name="T7">Type of the seventh action argument.</typeparam>
/// <typeparam name="T8">Type of the eighth action argument.</typeparam>
/// <typeparam name="T9">Type of the ninth action argument.</typeparam>
/// <typeparam name="T10">Type of the tenth action argument.</typeparam>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="arg1">The first action argument.</param>
/// <param name="arg2">The second action argument.</param>
/// <param name="arg3">The third action argument.</param>
/// <param name="arg4">The fourth action argument.</param>
/// <param name="arg5">The fifth action argument.</param>
/// <param name="arg6">The sixth action argument.</param>
/// <param name="arg7">The seventh action argument.</param>
/// <param name="arg8">The eighth action argument.</param>
/// <param name="arg9">The ninth action argument.</param>
/// <param name="arg10">The tenth action argument.</param>
/// <param name="token">Invocation cancellation token.</param>
/// <returns>The task representing state of asynchronous invocation.</returns>
public static unsafe Task InvokeAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, CancellationToken token = default)
{
return InvokeAsync(action, (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10), &Invoke, token);
static void Invoke(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action, in (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) args)
=> action(args.Item1, args.Item2, args.Item3, args.Item4, args.Item5, args.Item6, args.Item7, args.Item8, args.Item9, args.Item10);
}
/// <summary>
/// Invokes synchronous delegate asynchronously.
/// </summary>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="state">The state object to be passed to the action.</param>
/// <param name="callback">The callback to be invoked on completion.</param>
/// <param name="options">The task scheduling options.</param>
/// <param name="scheduler">The task scheduler.</param>
/// <returns>The task representing asynchronous execution of the action.</returns>
public static Task BeginInvoke(this Action<object?> action, object? state, AsyncCallback? callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler? scheduler = null)
{
var task = Task.Factory.StartNew(action, state, CancellationToken.None, options, scheduler ?? TaskScheduler.Default);
if (callback is not null)
task.OnCompleted(callback);
return task;
}
/// <summary>
/// Invokes synchronous delegate asynchronously.
/// </summary>
/// <param name="action">The action to invoke asynchronously.</param>
/// <param name="state">The state object to be passed to the action.</param>
/// <param name="callback">The callback to be invoked on completion.</param>
/// <param name="options">The task scheduling options.</param>
/// <param name="scheduler">The task scheduler.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>The task representing asynchronous execution of the action.</returns>
public static Task BeginInvoke(this Action<object?, CancellationToken> action, object? state, AsyncCallback? callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler? scheduler = null, CancellationToken token = default)
{
var task = Task.Factory.StartNew(s => action(s, token), state, token, options, scheduler ?? TaskScheduler.Default);
if (callback is not null)
task.OnCompleted(callback);
return task;
}
/// <summary>
/// Invokes synchronous delegate asynchronously.
/// </summary>
/// <typeparam name="TResult">The type of result of asynchronous operation.</typeparam>
/// <param name="function">The function to invoke asynchronously.</param>
/// <param name="state">The state object to be passed to the action.</param>
/// <param name="callback">The callback to be invoked on completion.</param>
/// <param name="options">The task scheduling options.</param>
/// <param name="scheduler">The task scheduler.</param>
/// <returns>The task representing asynchronous execution of the action.</returns>
public static Task<TResult> BeginInvoke<TResult>(this Func<object?, TResult> function, object? state, AsyncCallback? callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler? scheduler = null)
{
var task = Task<TResult>.Factory.StartNew(function, state, CancellationToken.None, options, scheduler ?? TaskScheduler.Default);
if (callback is not null)
task.OnCompleted(callback);
return task;
}
/// <summary>
/// Invokes synchronous delegate asynchronously.
/// </summary>
/// <typeparam name="TResult">The type of result of asynchronous operation.</typeparam>
/// <param name="function">The function to invoke asynchronously.</param>
/// <param name="state">The state object to be passed to the action.</param>
/// <param name="callback">The callback to be invoked on completion.</param>
/// <param name="options">The task scheduling options.</param>
/// <param name="scheduler">The task scheduler.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>The task representing asynchronous execution of the action.</returns>
public static Task<TResult> BeginInvoke<TResult>(this Func<object?, CancellationToken, TResult> function, object? state, AsyncCallback? callback, TaskCreationOptions options = TaskCreationOptions.None, TaskScheduler? scheduler = null, CancellationToken token = default)
{
var task = Task<TResult>.Factory.StartNew(s => function(s, token), state, token, options, scheduler ?? TaskScheduler.Default);
if (callback is not null)
task.OnCompleted(callback);
return task;
}
}
}