/
FutureFactory.cs
2103 lines (1926 loc) · 127 KB
/
FutureFactory.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
namespace System.Threading.Tasks
{
/// <summary>
/// Provides support for creating and scheduling
/// <see cref="System.Threading.Tasks.Task{TResult}">Task{TResult}</see> objects.
/// </summary>
/// <typeparam name="TResult">The type of the results that are available though
/// the <see cref="System.Threading.Tasks.Task{TResult}">Task{TResult}</see> objects that are associated with
/// the methods in this class.</typeparam>
/// <remarks>
/// <para>
/// There are many common patterns for which tasks are relevant. The <see cref="TaskFactory{TResult}"/>
/// class encodes some of these patterns into methods that pick up default settings, which are
/// configurable through its constructors.
/// </para>
/// <para>
/// A default instance of <see cref="TaskFactory{TResult}"/> is available through the
/// <see cref="System.Threading.Tasks.Task{TResult}.Factory">Task{TResult}.Factory</see> property.
/// </para>
/// </remarks>
public class TaskFactory<TResult>
{
// Member variables, DefaultScheduler, other properties and ctors
// copied right out of TaskFactory... Lots of duplication here...
// Should we be thinking about a TaskFactoryBase class?
// member variables
private readonly CancellationToken m_defaultCancellationToken;
private readonly TaskScheduler? m_defaultScheduler;
private readonly TaskCreationOptions m_defaultCreationOptions;
private readonly TaskContinuationOptions m_defaultContinuationOptions;
private TaskScheduler DefaultScheduler => m_defaultScheduler ?? TaskScheduler.Current;
// sister method to above property -- avoids a TLS lookup
private TaskScheduler GetDefaultScheduler(Task? currTask)
{
if (m_defaultScheduler != null) return m_defaultScheduler;
else if ((currTask != null)
&& ((currTask.CreationOptions & TaskCreationOptions.HideScheduler) == 0)
)
return currTask.ExecutingTaskScheduler!; // a "current" task must be executing, which means it must have a scheduler
else return TaskScheduler.Default;
}
/* Constructors */
/// <summary>
/// Initializes a <see cref="TaskFactory{TResult}"/> instance with the default configuration.
/// </summary>
/// <remarks>
/// This constructor creates a <see cref="TaskFactory{TResult}"/> instance with a default configuration. The
/// <see cref="TaskCreationOptions"/> property is initialized to
/// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
/// <see cref="TaskContinuationOptions"/> property is initialized to <see
/// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
/// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
/// initialized to the current scheduler (see <see
/// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
/// </remarks>
public TaskFactory()
{
}
/// <summary>
/// Initializes a <see cref="TaskFactory{TResult}"/> instance with the default configuration.
/// </summary>
/// <param name="cancellationToken">The default <see cref="CancellationToken"/> that will be assigned
/// to tasks created by this <see cref="TaskFactory"/> unless another CancellationToken is explicitly specified
/// while calling the factory methods.</param>
/// <remarks>
/// This constructor creates a <see cref="TaskFactory{TResult}"/> instance with a default configuration. The
/// <see cref="TaskCreationOptions"/> property is initialized to
/// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
/// <see cref="TaskContinuationOptions"/> property is initialized to <see
/// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
/// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
/// initialized to the current scheduler (see <see
/// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
/// </remarks>
public TaskFactory(CancellationToken cancellationToken)
{
m_defaultCancellationToken = cancellationToken;
}
/// <summary>
/// Initializes a <see cref="TaskFactory{TResult}"/> instance with the specified configuration.
/// </summary>
/// <param name="scheduler">
/// The <see cref="System.Threading.Tasks.TaskScheduler">
/// TaskScheduler</see> to use to schedule any tasks created with this TaskFactory{TResult}. A null value
/// indicates that the current TaskScheduler should be used.
/// </param>
/// <remarks>
/// With this constructor, the
/// <see cref="TaskCreationOptions"/> property is initialized to
/// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
/// <see cref="TaskContinuationOptions"/> property is initialized to <see
/// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
/// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
/// initialized to <paramref name="scheduler"/>, unless it's null, in which case the property is
/// initialized to the current scheduler (see <see
/// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
/// </remarks>
public TaskFactory(TaskScheduler? scheduler) // null means to use TaskScheduler.Current
{
m_defaultScheduler = scheduler;
}
/// <summary>
/// Initializes a <see cref="TaskFactory{TResult}"/> instance with the specified configuration.
/// </summary>
/// <param name="creationOptions">
/// The default <see cref="System.Threading.Tasks.TaskCreationOptions">
/// TaskCreationOptions</see> to use when creating tasks with this TaskFactory{TResult}.
/// </param>
/// <param name="continuationOptions">
/// The default <see cref="System.Threading.Tasks.TaskContinuationOptions">
/// TaskContinuationOptions</see> to use when creating continuation tasks with this TaskFactory{TResult}.
/// </param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// The exception that is thrown when the
/// <paramref name="creationOptions"/> argument or the <paramref name="continuationOptions"/>
/// argument specifies an invalid value.
/// </exception>
/// <remarks>
/// With this constructor, the
/// <see cref="TaskCreationOptions"/> property is initialized to <paramref name="creationOptions"/>,
/// the
/// <see cref="TaskContinuationOptions"/> property is initialized to <paramref
/// name="continuationOptions"/>, and the <see
/// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is initialized to the
/// current scheduler (see <see
/// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
/// </remarks>
public TaskFactory(TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
{
TaskFactory.CheckMultiTaskContinuationOptions(continuationOptions);
TaskFactory.CheckCreationOptions(creationOptions);
m_defaultCreationOptions = creationOptions;
m_defaultContinuationOptions = continuationOptions;
}
/// <summary>
/// Initializes a <see cref="TaskFactory{TResult}"/> instance with the specified configuration.
/// </summary>
/// <param name="cancellationToken">The default <see cref="CancellationToken"/> that will be assigned
/// to tasks created by this <see cref="TaskFactory"/> unless another CancellationToken is explicitly specified
/// while calling the factory methods.</param>
/// <param name="creationOptions">
/// The default <see cref="System.Threading.Tasks.TaskCreationOptions">
/// TaskCreationOptions</see> to use when creating tasks with this TaskFactory{TResult}.
/// </param>
/// <param name="continuationOptions">
/// The default <see cref="System.Threading.Tasks.TaskContinuationOptions">
/// TaskContinuationOptions</see> to use when creating continuation tasks with this TaskFactory{TResult}.
/// </param>
/// <param name="scheduler">
/// The default <see cref="System.Threading.Tasks.TaskScheduler">
/// TaskScheduler</see> to use to schedule any Tasks created with this TaskFactory{TResult}. A null value
/// indicates that TaskScheduler.Current should be used.
/// </param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// The exception that is thrown when the
/// <paramref name="creationOptions"/> argument or the <paramref name="continuationOptions"/>
/// argumentspecifies an invalid value.
/// </exception>
/// <remarks>
/// With this constructor, the
/// <see cref="TaskCreationOptions"/> property is initialized to <paramref name="creationOptions"/>,
/// the
/// <see cref="TaskContinuationOptions"/> property is initialized to <paramref
/// name="continuationOptions"/>, and the <see
/// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is initialized to
/// <paramref name="scheduler"/>, unless it's null, in which case the property is initialized to the
/// current scheduler (see <see
/// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
/// </remarks>
public TaskFactory(CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler? scheduler)
: this(creationOptions, continuationOptions)
{
m_defaultCancellationToken = cancellationToken;
m_defaultScheduler = scheduler;
}
/* Properties */
/// <summary>
/// Gets the default <see cref="System.Threading.CancellationToken">CancellationToken</see> of this
/// TaskFactory.
/// </summary>
/// <remarks>
/// This property returns the default <see cref="CancellationToken"/> that will be assigned to all
/// tasks created by this factory unless another CancellationToken value is explicitly specified
/// during the call to the factory methods.
/// </remarks>
public CancellationToken CancellationToken => m_defaultCancellationToken;
/// <summary>
/// Gets the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> of this
/// TaskFactory{TResult}.
/// </summary>
/// <remarks>
/// This property returns the default scheduler for this factory. It will be used to schedule all
/// tasks unless another scheduler is explicitly specified during calls to this factory's methods.
/// If null, <see cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>
/// will be used.
/// </remarks>
public TaskScheduler? Scheduler => m_defaultScheduler;
/// <summary>
/// Gets the <see cref="System.Threading.Tasks.TaskCreationOptions">TaskCreationOptions
/// </see> value of this TaskFactory{TResult}.
/// </summary>
/// <remarks>
/// This property returns the default creation options for this factory. They will be used to create all
/// tasks unless other options are explicitly specified during calls to this factory's methods.
/// </remarks>
public TaskCreationOptions CreationOptions => m_defaultCreationOptions;
/// <summary>
/// Gets the <see cref="System.Threading.Tasks.TaskCreationOptions">TaskContinuationOptions
/// </see> value of this TaskFactory{TResult}.
/// </summary>
/// <remarks>
/// This property returns the default continuation options for this factory. They will be used to create
/// all continuation tasks unless other options are explicitly specified during calls to this factory's methods.
/// </remarks>
public TaskContinuationOptions ContinuationOptions => m_defaultContinuationOptions;
/* StartNew */
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<TResult> function)
{
Task? currTask = Task.InternalCurrent;
return Task<TResult>.StartNew(currTask, function, m_defaultCancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
}
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <exception cref="System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
/// has already been disposed.
/// </exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<TResult> function, CancellationToken cancellationToken)
{
Task? currTask = Task.InternalCurrent;
return Task<TResult>.StartNew(currTask, function, cancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
}
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
/// created
/// <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<TResult> function, TaskCreationOptions creationOptions)
{
Task? currTask = Task.InternalCurrent;
return Task<TResult>.StartNew(currTask, function, m_defaultCancellationToken,
creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
}
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
/// created
/// <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
/// <param name="scheduler">The <see
/// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
/// that is used to schedule the created <see cref="System.Threading.Tasks.Task{TResult}">
/// Task{TResult}</see>.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="scheduler"/>
/// argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <exception cref="System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
/// has already been disposed.
/// </exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
{
return Task<TResult>.StartNew(
Task.InternalCurrentIfAttached(creationOptions), function, cancellationToken,
creationOptions, InternalTaskOptions.None, scheduler);
}
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="state">An object containing data to be used by the <paramref name="function"/>
/// delegate.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<object?, TResult> function, object? state)
{
Task? currTask = Task.InternalCurrent;
return Task<TResult>.StartNew(currTask, function, state, m_defaultCancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
}
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="state">An object containing data to be used by the <paramref name="function"/>
/// delegate.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <exception cref="System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
/// has already been disposed.
/// </exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<object?, TResult> function, object? state, CancellationToken cancellationToken)
{
Task? currTask = Task.InternalCurrent;
return Task<TResult>.StartNew(currTask, function, state, cancellationToken,
m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
}
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="state">An object containing data to be used by the <paramref name="function"/>
/// delegate.</param>
/// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
/// created
/// <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<object?, TResult> function, object? state, TaskCreationOptions creationOptions)
{
Task? currTask = Task.InternalCurrent;
return Task<TResult>.StartNew(currTask, function, state, m_defaultCancellationToken,
creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
}
/// <summary>
/// Creates and starts a <see cref="System.Threading.Tasks.Task{TResult}"/>.
/// </summary>
/// <param name="function">A function delegate that returns the future result to be available through
/// the <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="state">An object containing data to be used by the <paramref name="function"/>
/// delegate.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
/// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
/// created
/// <see cref="System.Threading.Tasks.Task{TResult}"/>.</param>
/// <param name="scheduler">The <see
/// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
/// that is used to schedule the created <see cref="System.Threading.Tasks.Task{TResult}">
/// Task{TResult}</see>.</param>
/// <returns>The started <see cref="System.Threading.Tasks.Task{TResult}"/>.</returns>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="function"/>
/// argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the <paramref
/// name="scheduler"/>
/// argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <exception cref="System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
/// has already been disposed.
/// </exception>
/// <remarks>
/// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
/// of its constructors and then calling
/// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
/// However, unless creation and scheduling must be separated, StartNew is the recommended approach
/// for both simplicity and performance.
/// </remarks>
public Task<TResult> StartNew(Func<object?, TResult> function, object? state, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
{
return Task<TResult>.StartNew(Task.InternalCurrentIfAttached(creationOptions), function, state, cancellationToken,
creationOptions, InternalTaskOptions.None, scheduler);
}
//
// APM Factory methods
//
// Common core logic for FromAsync calls. This minimizes the chance of "drift" between overload implementations.
private static void FromAsyncCoreLogic(
IAsyncResult iar,
Func<IAsyncResult, TResult>? endFunction,
Action<IAsyncResult>? endAction,
Task<TResult> promise,
bool requiresSynchronization)
{
Debug.Assert((endFunction != null) != (endAction != null), "Expected exactly one of endFunction/endAction to be non-null");
Exception? ex = null;
OperationCanceledException? oce = null;
TResult result = default!;
try
{
if (endFunction != null)
{
result = endFunction(iar);
}
else
{
endAction!(iar);
}
}
catch (OperationCanceledException _oce) { oce = _oce; }
catch (Exception e) { ex = e; }
finally
{
if (oce != null)
{
promise.TrySetCanceled(oce.CancellationToken, oce);
}
else if (ex != null)
{
promise.TrySetException(ex);
}
else
{
if (TplEventSource.Log.IsEnabled())
TplEventSource.Log.TraceOperationEnd(promise.Id, AsyncCausalityStatus.Completed);
if (Task.s_asyncDebuggingEnabled)
Task.RemoveFromActiveTasks(promise);
if (requiresSynchronization)
{
promise.TrySetResult(result);
}
else
{
promise.DangerousSetResult(result);
}
}
}
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
/// method function when a specified <see cref="System.IAsyncResult">IAsyncResult</see> completes.
/// </summary>
/// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
/// <paramref name="endMethod"/>.</param>
/// <param name="endMethod">The function delegate that processes the completed <paramref
/// name="asyncResult"/>.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="asyncResult"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents the
/// asynchronous operation.</returns>
public Task<TResult> FromAsync(IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
{
return FromAsyncImpl(asyncResult, endMethod, null, m_defaultCreationOptions, DefaultScheduler);
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
/// method function when a specified <see cref="System.IAsyncResult">IAsyncResult</see> completes.
/// </summary>
/// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
/// <paramref name="endMethod"/>.</param>
/// <param name="endMethod">The function delegate that processes the completed <paramref
/// name="asyncResult"/>.</param>
/// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
/// created <see cref="System.Threading.Tasks.Task{TResult}">Task</see>.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="asyncResult"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents the
/// asynchronous operation.</returns>
public Task<TResult> FromAsync(
IAsyncResult asyncResult,
Func<IAsyncResult, TResult> endMethod,
TaskCreationOptions creationOptions)
{
return FromAsyncImpl(asyncResult, endMethod, null, creationOptions, DefaultScheduler);
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
/// method function when a specified <see cref="System.IAsyncResult">IAsyncResult</see> completes.
/// </summary>
/// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
/// <paramref name="endMethod"/>.</param>
/// <param name="endMethod">The function delegate that processes the completed <paramref
/// name="asyncResult"/>.</param>
/// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
/// that is used to schedule the task that executes the end method.</param>
/// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
/// created <see cref="System.Threading.Tasks.Task{TResult}">Task</see>.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="asyncResult"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="scheduler"/> argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents the
/// asynchronous operation.</returns>
public Task<TResult> FromAsync(
IAsyncResult asyncResult,
Func<IAsyncResult, TResult> endMethod,
TaskCreationOptions creationOptions,
TaskScheduler scheduler)
{
return FromAsyncImpl(asyncResult, endMethod, null, creationOptions, scheduler);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory<TResult> instance.
internal static Task<TResult> FromAsyncImpl(
IAsyncResult asyncResult,
Func<IAsyncResult, TResult>? endFunction,
Action<IAsyncResult>? endAction,
TaskCreationOptions creationOptions,
TaskScheduler scheduler)
{
if (asyncResult == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.asyncResult);
if (endFunction == null && endAction == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.endMethod);
Debug.Assert((endFunction != null) != (endAction != null), "Both endFunction and endAction were non-null");
if (scheduler == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.scheduler);
TaskFactory.CheckFromAsyncOptions(creationOptions, false);
Task<TResult> promise = new Task<TResult>((object?)null, creationOptions);
if (TplEventSource.Log.IsEnabled())
TplEventSource.Log.TraceOperationBegin(promise.Id, "TaskFactory.FromAsync", 0);
if (Task.s_asyncDebuggingEnabled)
Task.AddToActiveTasks(promise);
// Just specify this task as detached. No matter what happens, we want endMethod
// to be called -- even if the parent is canceled. So we don't want to flow
// RespectParentCancellation.
Task t = new Task(new Action<object>(delegate
{
FromAsyncCoreLogic(asyncResult, endFunction, endAction, promise, requiresSynchronization: true);
}),
(object?)null, null,
default, TaskCreationOptions.None, InternalTaskOptions.None, null);
if (TplEventSource.Log.IsEnabled())
TplEventSource.Log.TraceOperationBegin(t.Id, "TaskFactory.FromAsync Callback", 0);
if (Task.s_asyncDebuggingEnabled)
Task.AddToActiveTasks(t);
if (asyncResult.IsCompleted)
{
try { t.InternalRunSynchronously(scheduler, waitForCompletion: false); }
catch (Exception e) { promise.TrySetException(e); } // catch and log any scheduler exceptions
}
else
{
#pragma warning disable CA1416 // Validate platform compatibility, issue: https://github.com/dotnet/runtime/issues/44544
ThreadPool.RegisterWaitForSingleObject(
asyncResult.AsyncWaitHandle,
delegate
{
try { t.InternalRunSynchronously(scheduler, waitForCompletion: false); }
catch (Exception e) { promise.TrySetException(e); } // catch and log any scheduler exceptions
},
null,
Timeout.Infinite,
true);
#pragma warning restore CA1416
}
return promise;
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
/// </summary>
/// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
/// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
/// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
/// delegate.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="beginMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <returns>The created <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that
/// represents the asynchronous operation.</returns>
/// <remarks>
/// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
/// </remarks>
public Task<TResult> FromAsync(
Func<AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod, object? state)
{
return FromAsyncImpl(beginMethod, endMethod, null, state, m_defaultCreationOptions);
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
/// </summary>
/// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
/// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
/// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
/// created <see cref="System.Threading.Tasks.Task{TResult}">Task</see>.</param>
/// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
/// delegate.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="beginMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <returns>The created <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that
/// represents the asynchronous operation.</returns>
/// <remarks>
/// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
/// </remarks>
public Task<TResult> FromAsync(
Func<AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod, object? state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, null, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory<TResult> instance.
internal static Task<TResult> FromAsyncImpl(Func<AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult>? endFunction, Action<IAsyncResult>? endAction,
object? state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.beginMethod);
if (endFunction == null && endAction == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.endMethod);
Debug.Assert((endFunction != null) != (endAction != null), "Both endFunction and endAction were non-null");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
Task<TResult> promise = new Task<TResult>(state, creationOptions);
if (TplEventSource.Log.IsEnabled())
TplEventSource.Log.TraceOperationBegin(promise.Id, "TaskFactory.FromAsync: " + beginMethod.Method.Name, 0);
if (Task.s_asyncDebuggingEnabled)
Task.AddToActiveTasks(promise);
try
{
// if we don't require synchronization, a faster set result path is taken
IAsyncResult asyncResult = beginMethod(iar =>
{
if (!iar.CompletedSynchronously)
FromAsyncCoreLogic(iar, endFunction, endAction, promise, requiresSynchronization: true);
}, state);
if (asyncResult.CompletedSynchronously)
{
Debug.Assert(asyncResult.IsCompleted, "If the operation completed synchronously, it must be completed.");
FromAsyncCoreLogic(asyncResult, endFunction, endAction, promise, requiresSynchronization: false);
}
}
catch
{
if (TplEventSource.Log.IsEnabled())
TplEventSource.Log.TraceOperationEnd(promise.Id, AsyncCausalityStatus.Error);
if (Task.s_asyncDebuggingEnabled)
Task.RemoveFromActiveTasks(promise);
// Make sure we don't leave promise "dangling".
promise.TrySetResult();
throw;
}
return promise;
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
/// </summary>
/// <typeparam name="TArg1">The type of the first argument passed to the <paramref
/// name="beginMethod"/> delegate.</typeparam>
/// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
/// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
/// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
/// delegate.</param>
/// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
/// delegate.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="beginMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <returns>The created <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that
/// represents the asynchronous operation.</returns>
/// <remarks>
/// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
/// </remarks>
public Task<TResult> FromAsync<TArg1>(
Func<TArg1, AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod,
TArg1 arg1, object? state)
{
return FromAsyncImpl(beginMethod, endMethod, null, arg1, state, m_defaultCreationOptions);
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
/// </summary>
/// <typeparam name="TArg1">The type of the first argument passed to the <paramref
/// name="beginMethod"/> delegate.</typeparam>
/// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
/// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
/// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
/// delegate.</param>
/// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
/// created <see cref="System.Threading.Tasks.Task{TResult}">Task</see>.</param>
/// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
/// delegate.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="beginMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <returns>The created <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that
/// represents the asynchronous operation.</returns>
/// <remarks>
/// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
/// </remarks>
public Task<TResult> FromAsync<TArg1>(
Func<TArg1, AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod,
TArg1 arg1, object? state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, null, arg1, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory<TResult> instance.
internal static Task<TResult> FromAsyncImpl<TArg1>(Func<TArg1, AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult>? endFunction, Action<IAsyncResult>? endAction,
TArg1 arg1, object? state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.beginMethod);
if (endFunction == null && endAction == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.endFunction);
Debug.Assert((endFunction != null) != (endAction != null), "Both endFunction and endAction were non-null");
TaskFactory.CheckFromAsyncOptions(creationOptions, true);
Task<TResult> promise = new Task<TResult>(state, creationOptions);
if (TplEventSource.Log.IsEnabled())
TplEventSource.Log.TraceOperationBegin(promise.Id, "TaskFactory.FromAsync: " + beginMethod.Method.Name, 0);
if (Task.s_asyncDebuggingEnabled)
Task.AddToActiveTasks(promise);
try
{
// if we don't require synchronization, a faster set result path is taken
IAsyncResult asyncResult = beginMethod(arg1, iar =>
{
if (!iar.CompletedSynchronously)
FromAsyncCoreLogic(iar, endFunction, endAction, promise, requiresSynchronization: true);
}, state);
if (asyncResult.CompletedSynchronously)
{
Debug.Assert(asyncResult.IsCompleted, "If the operation completed synchronously, it must be completed.");
FromAsyncCoreLogic(asyncResult, endFunction, endAction, promise, requiresSynchronization: false);
}
}
catch
{
if (TplEventSource.Log.IsEnabled())
TplEventSource.Log.TraceOperationEnd(promise.Id, AsyncCausalityStatus.Error);
if (Task.s_asyncDebuggingEnabled)
Task.RemoveFromActiveTasks(promise);
// Make sure we don't leave promise "dangling".
promise.TrySetResult();
throw;
}
return promise;
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
/// </summary>
/// <typeparam name="TArg1">The type of the first argument passed to the <paramref
/// name="beginMethod"/> delegate.</typeparam>
/// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
/// delegate.</typeparam>
/// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
/// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
/// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
/// delegate.</param>
/// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
/// delegate.</param>
/// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
/// delegate.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="beginMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <returns>The created <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that
/// represents the asynchronous operation.</returns>
/// <remarks>
/// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
/// </remarks>
public Task<TResult> FromAsync<TArg1, TArg2>(
Func<TArg1, TArg2, AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod,
TArg1 arg1, TArg2 arg2, object? state)
{
return FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, state, m_defaultCreationOptions);
}
/// <summary>
/// Creates a <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
/// begin and end methods that conform to the Asynchronous Programming Model pattern.
/// </summary>
/// <typeparam name="TArg1">The type of the first argument passed to the <paramref
/// name="beginMethod"/> delegate.</typeparam>
/// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
/// delegate.</typeparam>
/// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
/// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
/// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
/// delegate.</param>
/// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
/// delegate.</param>
/// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
/// created <see cref="System.Threading.Tasks.Task{TResult}">Task</see>.</param>
/// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
/// delegate.</param>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="beginMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentNullException">The exception that is thrown when the
/// <paramref name="endMethod"/> argument is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">The exception that is thrown when the
/// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
/// value.</exception>
/// <returns>The created <see cref="System.Threading.Tasks.Task{TResult}">Task</see> that
/// represents the asynchronous operation.</returns>
/// <remarks>
/// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
/// </remarks>
public Task<TResult> FromAsync<TArg1, TArg2>(
Func<TArg1, TArg2, AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod,
TArg1 arg1, TArg2 arg2, object? state, TaskCreationOptions creationOptions)
{
return FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, state, creationOptions);
}
// We need this logic broken out into a static method so that the similar TaskFactory.FromAsync()
// method can access the logic w/o declaring a TaskFactory<TResult> instance.
internal static Task<TResult> FromAsyncImpl<TArg1, TArg2>(Func<TArg1, TArg2, AsyncCallback, object?, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult>? endFunction, Action<IAsyncResult>? endAction,
TArg1 arg1, TArg2 arg2, object? state, TaskCreationOptions creationOptions)
{
if (beginMethod == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.beginMethod);
if (endFunction == null && endAction == null)