TaskFactory.cs 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  5. //
  6. //
  7. //
  8. // There are a plethora of common patterns for which Tasks are created. TaskFactory encodes
  9. // these patterns into helper methods. These helpers also pick up default configuration settings
  10. // applicable to the entire factory and configurable through its constructors.
  11. //
  12. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Security;
  16. using System.Runtime.CompilerServices;
  17. using System.Threading;
  18. using System.Diagnostics;
  19. using AsyncStatus = Internal.Runtime.Augments.AsyncStatus;
  20. using CausalityRelation = Internal.Runtime.Augments.CausalityRelation;
  21. using CausalitySource = Internal.Runtime.Augments.CausalitySource;
  22. using CausalityTraceLevel = Internal.Runtime.Augments.CausalityTraceLevel;
  23. using CausalitySynchronousWork = Internal.Runtime.Augments.CausalitySynchronousWork;
  24. namespace System.Threading.Tasks
  25. {
  26. /// <summary>
  27. /// Provides support for creating and scheduling
  28. /// <see cref="T:System.Threading.Tasks.Task">Tasks</see>.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// There are many common patterns for which tasks are relevant. The <see cref="TaskFactory"/>
  33. /// class encodes some of these patterns into methods that pick up default settings, which are
  34. /// configurable through its constructors.
  35. /// </para>
  36. /// <para>
  37. /// A default instance of <see cref="TaskFactory"/> is available through the
  38. /// <see cref="System.Threading.Tasks.Task.Factory">Task.Factory</see> property.
  39. /// </para>
  40. /// </remarks>
  41. public class TaskFactory
  42. {
  43. // member variables
  44. private readonly CancellationToken m_defaultCancellationToken;
  45. private readonly TaskScheduler m_defaultScheduler;
  46. private readonly TaskCreationOptions m_defaultCreationOptions;
  47. private readonly TaskContinuationOptions m_defaultContinuationOptions;
  48. private TaskScheduler DefaultScheduler => m_defaultScheduler ?? TaskScheduler.Current;
  49. // sister method to above property -- avoids a TLS lookup
  50. private TaskScheduler GetDefaultScheduler(Task currTask)
  51. {
  52. return
  53. m_defaultScheduler ??
  54. (currTask != null && (currTask.CreationOptions & TaskCreationOptions.HideScheduler) == 0 ? currTask.ExecutingTaskScheduler :
  55. TaskScheduler.Default);
  56. }
  57. /* Constructors */
  58. // ctor parameters provide defaults for the factory, which can be overridden by options provided to
  59. // specific calls on the factory
  60. /// <summary>
  61. /// Initializes a <see cref="TaskFactory"/> instance with the default configuration.
  62. /// </summary>
  63. /// <remarks>
  64. /// This constructor creates a <see cref="TaskFactory"/> instance with a default configuration. The
  65. /// <see cref="TaskCreationOptions"/> property is initialized to
  66. /// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
  67. /// <see cref="TaskContinuationOptions"/> property is initialized to <see
  68. /// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
  69. /// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
  70. /// initialized to the current scheduler (see <see
  71. /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
  72. /// </remarks>
  73. public TaskFactory()
  74. : this(default, TaskCreationOptions.None, TaskContinuationOptions.None, null)
  75. {
  76. }
  77. /// <summary>
  78. /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
  79. /// </summary>
  80. /// <param name="cancellationToken">The default <see cref="CancellationToken"/> that will be assigned
  81. /// to tasks created by this <see cref="TaskFactory"/> unless another CancellationToken is explicitly specified
  82. /// while calling the factory methods.</param>
  83. /// <remarks>
  84. /// This constructor creates a <see cref="TaskFactory"/> instance with a default configuration. The
  85. /// <see cref="TaskCreationOptions"/> property is initialized to
  86. /// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
  87. /// <see cref="TaskContinuationOptions"/> property is initialized to <see
  88. /// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
  89. /// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
  90. /// initialized to the current scheduler (see <see
  91. /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
  92. /// </remarks>
  93. public TaskFactory(CancellationToken cancellationToken)
  94. : this(cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
  95. {
  96. }
  97. /// <summary>
  98. /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
  99. /// </summary>
  100. /// <param name="scheduler">
  101. /// The <see cref="System.Threading.Tasks.TaskScheduler">
  102. /// TaskScheduler</see> to use to schedule any tasks created with this TaskFactory. A null value
  103. /// indicates that the current TaskScheduler should be used.
  104. /// </param>
  105. /// <remarks>
  106. /// With this constructor, the
  107. /// <see cref="TaskCreationOptions"/> property is initialized to
  108. /// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
  109. /// <see cref="TaskContinuationOptions"/> property is initialized to <see
  110. /// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
  111. /// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
  112. /// initialized to <paramref name="scheduler"/>, unless it's null, in which case the property is
  113. /// initialized to the current scheduler (see <see
  114. /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
  115. /// </remarks>
  116. public TaskFactory(TaskScheduler scheduler) // null means to use TaskScheduler.Current
  117. : this(default, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  118. {
  119. }
  120. /// <summary>
  121. /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
  122. /// </summary>
  123. /// <param name="creationOptions">
  124. /// The default <see cref="System.Threading.Tasks.TaskCreationOptions">
  125. /// TaskCreationOptions</see> to use when creating tasks with this TaskFactory.
  126. /// </param>
  127. /// <param name="continuationOptions">
  128. /// The default <see cref="System.Threading.Tasks.TaskContinuationOptions">
  129. /// TaskContinuationOptions</see> to use when creating continuation tasks with this TaskFactory.
  130. /// </param>
  131. /// <exception cref="T:System.ArgumentOutOfRangeException">
  132. /// The exception that is thrown when the
  133. /// <paramref name="creationOptions"/> argument or the <paramref name="continuationOptions"/>
  134. /// argument specifies an invalid value.
  135. /// </exception>
  136. /// <remarks>
  137. /// With this constructor, the
  138. /// <see cref="TaskCreationOptions"/> property is initialized to <paramref name="creationOptions"/>,
  139. /// the
  140. /// <see cref="TaskContinuationOptions"/> property is initialized to <paramref
  141. /// name="continuationOptions"/>, and the <see
  142. /// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is initialized to the
  143. /// current scheduler (see <see
  144. /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
  145. /// </remarks>
  146. public TaskFactory(TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  147. : this(default, creationOptions, continuationOptions, null)
  148. {
  149. }
  150. /// <summary>
  151. /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
  152. /// </summary>
  153. /// <param name="cancellationToken">The default <see cref="CancellationToken"/> that will be assigned
  154. /// to tasks created by this <see cref="TaskFactory"/> unless another CancellationToken is explicitly specified
  155. /// while calling the factory methods.</param>
  156. /// <param name="creationOptions">
  157. /// The default <see cref="System.Threading.Tasks.TaskCreationOptions">
  158. /// TaskCreationOptions</see> to use when creating tasks with this TaskFactory.
  159. /// </param>
  160. /// <param name="continuationOptions">
  161. /// The default <see cref="System.Threading.Tasks.TaskContinuationOptions">
  162. /// TaskContinuationOptions</see> to use when creating continuation tasks with this TaskFactory.
  163. /// </param>
  164. /// <param name="scheduler">
  165. /// The default <see cref="System.Threading.Tasks.TaskScheduler">
  166. /// TaskScheduler</see> to use to schedule any Tasks created with this TaskFactory. A null value
  167. /// indicates that TaskScheduler.Current should be used.
  168. /// </param>
  169. /// <exception cref="T:System.ArgumentOutOfRangeException">
  170. /// The exception that is thrown when the
  171. /// <paramref name="creationOptions"/> argument or the <paramref name="continuationOptions"/>
  172. /// argumentspecifies an invalid value.
  173. /// </exception>
  174. /// <remarks>
  175. /// With this constructor, the
  176. /// <see cref="TaskCreationOptions"/> property is initialized to <paramref name="creationOptions"/>,
  177. /// the
  178. /// <see cref="TaskContinuationOptions"/> property is initialized to <paramref
  179. /// name="continuationOptions"/>, and the <see
  180. /// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is initialized to
  181. /// <paramref name="scheduler"/>, unless it's null, in which case the property is initialized to the
  182. /// current scheduler (see <see
  183. /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
  184. /// </remarks>
  185. public TaskFactory(CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  186. {
  187. CheckMultiTaskContinuationOptions(continuationOptions);
  188. CheckCreationOptions(creationOptions);
  189. m_defaultCancellationToken = cancellationToken;
  190. m_defaultScheduler = scheduler;
  191. m_defaultCreationOptions = creationOptions;
  192. m_defaultContinuationOptions = continuationOptions;
  193. }
  194. internal static void CheckCreationOptions(TaskCreationOptions creationOptions)
  195. {
  196. // Check for validity of options
  197. if ((creationOptions &
  198. ~(TaskCreationOptions.AttachedToParent |
  199. TaskCreationOptions.DenyChildAttach |
  200. TaskCreationOptions.HideScheduler |
  201. TaskCreationOptions.LongRunning |
  202. TaskCreationOptions.PreferFairness |
  203. TaskCreationOptions.RunContinuationsAsynchronously)) != 0)
  204. {
  205. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions);
  206. }
  207. }
  208. /* Properties */
  209. /// <summary>
  210. /// Gets the default <see cref="System.Threading.CancellationToken">CancellationToken</see> of this
  211. /// TaskFactory.
  212. /// </summary>
  213. /// <remarks>
  214. /// This property returns the default <see cref="CancellationToken"/> that will be assigned to all
  215. /// tasks created by this factory unless another CancellationToken value is explicitly specified
  216. /// during the call to the factory methods.
  217. /// </remarks>
  218. public CancellationToken CancellationToken { get { return m_defaultCancellationToken; } }
  219. /// <summary>
  220. /// Gets the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> of this
  221. /// TaskFactory.
  222. /// </summary>
  223. /// <remarks>
  224. /// This property returns the default scheduler for this factory. It will be used to schedule all
  225. /// tasks unless another scheduler is explicitly specified during calls to this factory's methods.
  226. /// If null, <see cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>
  227. /// will be used.
  228. /// </remarks>
  229. public TaskScheduler Scheduler { get { return m_defaultScheduler; } }
  230. /// <summary>
  231. /// Gets the <see cref="System.Threading.Tasks.TaskCreationOptions">TaskCreationOptions
  232. /// </see> value of this TaskFactory.
  233. /// </summary>
  234. /// <remarks>
  235. /// This property returns the default creation options for this factory. They will be used to create all
  236. /// tasks unless other options are explicitly specified during calls to this factory's methods.
  237. /// </remarks>
  238. public TaskCreationOptions CreationOptions { get { return m_defaultCreationOptions; } }
  239. /// <summary>
  240. /// Gets the <see cref="System.Threading.Tasks.TaskCreationOptions">TaskContinuationOptions
  241. /// </see> value of this TaskFactory.
  242. /// </summary>
  243. /// <remarks>
  244. /// This property returns the default continuation options for this factory. They will be used to create
  245. /// all continuation tasks unless other options are explicitly specified during calls to this factory's methods.
  246. /// </remarks>
  247. public TaskContinuationOptions ContinuationOptions { get { return m_defaultContinuationOptions; } }
  248. //
  249. // StartNew methods
  250. //
  251. /// <summary>
  252. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  253. /// </summary>
  254. /// <param name="action">The action delegate to execute asynchronously.</param>
  255. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  256. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref name="action"/>
  257. /// argument is null.</exception>
  258. /// <remarks>
  259. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors
  260. /// and then calling
  261. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution. However,
  262. /// unless creation and scheduling must be separated, StartNew is the recommended
  263. /// approach for both simplicity and performance.
  264. /// </remarks>
  265. public Task StartNew(Action action)
  266. {
  267. Task currTask = Task.InternalCurrent;
  268. return Task.InternalStartNew(currTask, action, null, m_defaultCancellationToken, GetDefaultScheduler(currTask),
  269. m_defaultCreationOptions, InternalTaskOptions.None);
  270. }
  271. /// <summary>
  272. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  273. /// </summary>
  274. /// <param name="action">The action delegate to execute asynchronously.</param>
  275. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
  276. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  277. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref name="action"/>
  278. /// argument is null.</exception>
  279. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  280. /// has already been disposed.
  281. /// </exception>
  282. /// <remarks>
  283. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors
  284. /// and then calling
  285. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution. However,
  286. /// unless creation and scheduling must be separated, StartNew is the recommended
  287. /// approach for both simplicity and performance.
  288. /// </remarks>
  289. public Task StartNew(Action action, CancellationToken cancellationToken)
  290. {
  291. Task currTask = Task.InternalCurrent;
  292. return Task.InternalStartNew(currTask, action, null, cancellationToken, GetDefaultScheduler(currTask),
  293. m_defaultCreationOptions, InternalTaskOptions.None);
  294. }
  295. /// <summary>
  296. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  297. /// </summary>
  298. /// <param name="action">The action delegate to execute asynchronously.</param>
  299. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  300. /// created
  301. /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
  302. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  303. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  304. /// name="action"/>
  305. /// argument is null.</exception>
  306. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  307. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  308. /// value.</exception>
  309. /// <remarks>
  310. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
  311. /// then calling
  312. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  313. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  314. /// for both simplicity and performance.
  315. /// </remarks>
  316. public Task StartNew(Action action, TaskCreationOptions creationOptions)
  317. {
  318. Task currTask = Task.InternalCurrent;
  319. return Task.InternalStartNew(currTask, action, null, m_defaultCancellationToken, GetDefaultScheduler(currTask), creationOptions,
  320. InternalTaskOptions.None);
  321. }
  322. /// <summary>
  323. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  324. /// </summary>
  325. /// <param name="action">The action delegate to execute asynchronously.</param>
  326. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
  327. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  328. /// created
  329. /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
  330. /// <param name="scheduler">The <see
  331. /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  332. /// that is used to schedule the created <see
  333. /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  334. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  335. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  336. /// name="action"/>
  337. /// argument is null.</exception>
  338. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  339. /// name="scheduler"/>
  340. /// argument is null.</exception>
  341. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  342. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  343. /// value.</exception>
  344. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  345. /// has already been disposed.
  346. /// </exception>
  347. /// <remarks>
  348. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
  349. /// then calling
  350. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  351. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  352. /// for both simplicity and performance.
  353. /// </remarks>
  354. public Task StartNew(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  355. {
  356. return Task.InternalStartNew(
  357. Task.InternalCurrentIfAttached(creationOptions), action, null, cancellationToken, scheduler, creationOptions,
  358. InternalTaskOptions.None);
  359. }
  360. /// <summary>
  361. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  362. /// </summary>
  363. /// <param name="action">The action delegate to execute asynchronously.</param>
  364. /// <param name="state">An object containing data to be used by the <paramref name="action"/>
  365. /// delegate.</param>
  366. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  367. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  368. /// name="action"/>
  369. /// argument is null.</exception>
  370. /// <remarks>
  371. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
  372. /// then calling
  373. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  374. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  375. /// for both simplicity and performance.
  376. /// </remarks>
  377. public Task StartNew(Action<object> action, object state)
  378. {
  379. Task currTask = Task.InternalCurrent;
  380. return Task.InternalStartNew(currTask, action, state, m_defaultCancellationToken, GetDefaultScheduler(currTask),
  381. m_defaultCreationOptions, InternalTaskOptions.None);
  382. }
  383. /// <summary>
  384. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  385. /// </summary>
  386. /// <param name="action">The action delegate to execute asynchronously.</param>
  387. /// <param name="state">An object containing data to be used by the <paramref name="action"/>
  388. /// delegate.</param>
  389. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
  390. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  391. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  392. /// name="action"/>
  393. /// argument is null.</exception>
  394. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  395. /// has already been disposed.
  396. /// </exception>
  397. /// <remarks>
  398. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
  399. /// then calling
  400. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  401. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  402. /// for both simplicity and performance.
  403. /// </remarks>
  404. public Task StartNew(Action<object> action, object state, CancellationToken cancellationToken)
  405. {
  406. Task currTask = Task.InternalCurrent;
  407. return Task.InternalStartNew(currTask, action, state, cancellationToken, GetDefaultScheduler(currTask),
  408. m_defaultCreationOptions, InternalTaskOptions.None);
  409. }
  410. /// <summary>
  411. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  412. /// </summary>
  413. /// <param name="action">The action delegate to execute asynchronously.</param>
  414. /// <param name="state">An object containing data to be used by the <paramref name="action"/>
  415. /// delegate.</param>
  416. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  417. /// created
  418. /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
  419. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  420. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  421. /// name="action"/>
  422. /// argument is null.</exception>
  423. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  424. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  425. /// value.</exception>
  426. /// <remarks>
  427. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
  428. /// then calling
  429. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  430. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  431. /// for both simplicity and performance.
  432. /// </remarks>
  433. public Task StartNew(Action<object> action, object state, TaskCreationOptions creationOptions)
  434. {
  435. Task currTask = Task.InternalCurrent;
  436. return Task.InternalStartNew(currTask, action, state, m_defaultCancellationToken, GetDefaultScheduler(currTask),
  437. creationOptions, InternalTaskOptions.None);
  438. }
  439. /// <summary>
  440. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
  441. /// </summary>
  442. /// <param name="action">The action delegate to execute asynchronously.</param>
  443. /// <param name="state">An object containing data to be used by the <paramref name="action"/>
  444. /// delegate.</param>
  445. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
  446. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  447. /// created
  448. /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
  449. /// <param name="scheduler">The <see
  450. /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  451. /// that is used to schedule the created <see
  452. /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  453. /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  454. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  455. /// name="action"/>
  456. /// argument is null.</exception>
  457. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  458. /// name="scheduler"/>
  459. /// argument is null.</exception>
  460. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  461. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  462. /// value.</exception>
  463. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  464. /// has already been disposed.
  465. /// </exception>
  466. /// <remarks>
  467. /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
  468. /// then calling
  469. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  470. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  471. /// for both simplicity and performance.
  472. /// </remarks>
  473. public Task StartNew(Action<object> action, object state, CancellationToken cancellationToken,
  474. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  475. {
  476. return Task.InternalStartNew(
  477. Task.InternalCurrentIfAttached(creationOptions), action, state, cancellationToken, scheduler,
  478. creationOptions, InternalTaskOptions.None);
  479. }
  480. /// <summary>
  481. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  482. /// </summary>
  483. /// <typeparam name="TResult">The type of the result available through the
  484. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  485. /// </typeparam>
  486. /// <param name="function">A function delegate that returns the future result to be available through
  487. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  488. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  489. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  490. /// name="function"/>
  491. /// argument is null.</exception>
  492. /// <remarks>
  493. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  494. /// of its constructors and then calling
  495. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  496. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  497. /// for both simplicity and performance.
  498. /// </remarks>
  499. public Task<TResult> StartNew<TResult>(Func<TResult> function)
  500. {
  501. Task currTask = Task.InternalCurrent;
  502. return Task<TResult>.StartNew(currTask, function, m_defaultCancellationToken,
  503. m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
  504. }
  505. /// <summary>
  506. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  507. /// </summary>
  508. /// <typeparam name="TResult">The type of the result available through the
  509. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  510. /// </typeparam>
  511. /// <param name="function">A function delegate that returns the future result to be available through
  512. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  513. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
  514. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  515. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  516. /// name="function"/>
  517. /// argument is null.</exception>
  518. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  519. /// has already been disposed.
  520. /// </exception>
  521. /// <remarks>
  522. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  523. /// of its constructors and then calling
  524. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  525. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  526. /// for both simplicity and performance.
  527. /// </remarks>
  528. public Task<TResult> StartNew<TResult>(Func<TResult> function, CancellationToken cancellationToken)
  529. {
  530. Task currTask = Task.InternalCurrent;
  531. return Task<TResult>.StartNew(currTask, function, cancellationToken,
  532. m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
  533. }
  534. /// <summary>
  535. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  536. /// </summary>
  537. /// <typeparam name="TResult">The type of the result available through the
  538. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  539. /// </typeparam>
  540. /// <param name="function">A function delegate that returns the future result to be available through
  541. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  542. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  543. /// created
  544. /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  545. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  546. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  547. /// name="function"/>
  548. /// argument is null.</exception>
  549. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  550. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  551. /// value.</exception>
  552. /// <remarks>
  553. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  554. /// of its constructors and then calling
  555. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  556. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  557. /// for both simplicity and performance.
  558. /// </remarks>
  559. public Task<TResult> StartNew<TResult>(Func<TResult> function, TaskCreationOptions creationOptions)
  560. {
  561. Task currTask = Task.InternalCurrent;
  562. return Task<TResult>.StartNew(currTask, function, m_defaultCancellationToken,
  563. creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
  564. }
  565. /// <summary>
  566. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  567. /// </summary>
  568. /// <typeparam name="TResult">The type of the result available through the
  569. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  570. /// </typeparam>
  571. /// <param name="function">A function delegate that returns the future result to be available through
  572. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  573. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
  574. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  575. /// created
  576. /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  577. /// <param name="scheduler">The <see
  578. /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  579. /// that is used to schedule the created <see cref="T:System.Threading.Tasks.Task{TResult}">
  580. /// Task{TResult}</see>.</param>
  581. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  582. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  583. /// name="function"/>
  584. /// argument is null.</exception>
  585. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  586. /// name="scheduler"/>
  587. /// argument is null.</exception>
  588. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  589. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  590. /// value.</exception>
  591. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  592. /// has already been disposed.
  593. /// </exception>
  594. /// <remarks>
  595. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  596. /// of its constructors and then calling
  597. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  598. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  599. /// for both simplicity and performance.
  600. /// </remarks>
  601. public Task<TResult> StartNew<TResult>(Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  602. {
  603. return Task<TResult>.StartNew(
  604. Task.InternalCurrentIfAttached(creationOptions), function, cancellationToken,
  605. creationOptions, InternalTaskOptions.None, scheduler);
  606. }
  607. /// <summary>
  608. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  609. /// </summary>
  610. /// <typeparam name="TResult">The type of the result available through the
  611. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  612. /// </typeparam>
  613. /// <param name="function">A function delegate that returns the future result to be available through
  614. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  615. /// <param name="state">An object containing data to be used by the <paramref name="function"/>
  616. /// delegate.</param>
  617. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  618. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  619. /// name="function"/>
  620. /// argument is null.</exception>
  621. /// <remarks>
  622. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  623. /// of its constructors and then calling
  624. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  625. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  626. /// for both simplicity and performance.
  627. /// </remarks>
  628. public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state)
  629. {
  630. Task currTask = Task.InternalCurrent;
  631. return Task<TResult>.StartNew(currTask, function, state, m_defaultCancellationToken,
  632. m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
  633. }
  634. /// <summary>
  635. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  636. /// </summary>
  637. /// <typeparam name="TResult">The type of the result available through the
  638. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  639. /// </typeparam>
  640. /// <param name="function">A function delegate that returns the future result to be available through
  641. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  642. /// <param name="state">An object containing data to be used by the <paramref name="function"/>
  643. /// delegate.</param>
  644. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
  645. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  646. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  647. /// name="function"/>
  648. /// argument is null.</exception>
  649. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  650. /// has already been disposed.
  651. /// </exception>
  652. /// <remarks>
  653. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  654. /// of its constructors and then calling
  655. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  656. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  657. /// for both simplicity and performance.
  658. /// </remarks>
  659. public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state, CancellationToken cancellationToken)
  660. {
  661. Task currTask = Task.InternalCurrent;
  662. return Task<TResult>.StartNew(currTask, function, state, cancellationToken,
  663. m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
  664. }
  665. /// <summary>
  666. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  667. /// </summary>
  668. /// <typeparam name="TResult">The type of the result available through the
  669. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  670. /// </typeparam>
  671. /// <param name="function">A function delegate that returns the future result to be available through
  672. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  673. /// <param name="state">An object containing data to be used by the <paramref name="function"/>
  674. /// delegate.</param>
  675. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  676. /// created
  677. /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  678. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  679. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  680. /// name="function"/>
  681. /// argument is null.</exception>
  682. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  683. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  684. /// value.</exception>
  685. /// <remarks>
  686. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  687. /// of its constructors and then calling
  688. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  689. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  690. /// for both simplicity and performance.
  691. /// </remarks>
  692. public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  693. {
  694. Task currTask = Task.InternalCurrent;
  695. return Task<TResult>.StartNew(currTask, function, state, m_defaultCancellationToken,
  696. creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
  697. }
  698. /// <summary>
  699. /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
  700. /// </summary>
  701. /// <typeparam name="TResult">The type of the result available through the
  702. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  703. /// </typeparam>
  704. /// <param name="function">A function delegate that returns the future result to be available through
  705. /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  706. /// <param name="state">An object containing data to be used by the <paramref name="function"/>
  707. /// delegate.</param>
  708. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
  709. /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
  710. /// created
  711. /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  712. /// <param name="scheduler">The <see
  713. /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  714. /// that is used to schedule the created <see cref="T:System.Threading.Tasks.Task{TResult}">
  715. /// Task{TResult}</see>.</param>
  716. /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  717. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  718. /// name="function"/>
  719. /// argument is null.</exception>
  720. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
  721. /// name="scheduler"/>
  722. /// argument is null.</exception>
  723. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  724. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  725. /// value.</exception>
  726. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  727. /// has already been disposed.
  728. /// </exception>
  729. /// <remarks>
  730. /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
  731. /// of its constructors and then calling
  732. /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
  733. /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
  734. /// for both simplicity and performance.
  735. /// </remarks>
  736. public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state, CancellationToken cancellationToken,
  737. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  738. {
  739. return Task<TResult>.StartNew(
  740. Task.InternalCurrentIfAttached(creationOptions), function, state, cancellationToken,
  741. creationOptions, InternalTaskOptions.None, scheduler);
  742. }
  743. //
  744. // FromAsync methods
  745. //
  746. /// <summary>
  747. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that executes an end method action
  748. /// when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
  749. /// </summary>
  750. /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
  751. /// <paramref name="endMethod"/>.</param>
  752. /// <param name="endMethod">The action delegate that processes the completed <paramref
  753. /// name="asyncResult"/>.</param>
  754. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  755. /// <paramref name="asyncResult"/> argument is null.</exception>
  756. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  757. /// <paramref name="endMethod"/> argument is null.</exception>
  758. /// <returns>A <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the asynchronous
  759. /// operation.</returns>
  760. public Task FromAsync(
  761. IAsyncResult asyncResult,
  762. Action<IAsyncResult> endMethod)
  763. {
  764. return FromAsync(asyncResult, endMethod, m_defaultCreationOptions, DefaultScheduler);
  765. }
  766. /// <summary>
  767. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that executes an end method action
  768. /// when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
  769. /// </summary>
  770. /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
  771. /// <paramref name="endMethod"/>.</param>
  772. /// <param name="endMethod">The action delegate that processes the completed <paramref
  773. /// name="asyncResult"/>.</param>
  774. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  775. /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  776. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  777. /// <paramref name="asyncResult"/> argument is null.</exception>
  778. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  779. /// <paramref name="endMethod"/> argument is null.</exception>
  780. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  781. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  782. /// value.</exception>
  783. /// <returns>A <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the asynchronous
  784. /// operation.</returns>
  785. public Task FromAsync(
  786. IAsyncResult asyncResult,
  787. Action<IAsyncResult> endMethod,
  788. TaskCreationOptions creationOptions)
  789. {
  790. return FromAsync(asyncResult, endMethod, creationOptions, DefaultScheduler);
  791. }
  792. /// <summary>
  793. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that executes an end method action
  794. /// when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
  795. /// </summary>
  796. /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
  797. /// <paramref name="endMethod"/>.</param>
  798. /// <param name="endMethod">The action delegate that processes the completed <paramref
  799. /// name="asyncResult"/>.</param>
  800. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  801. /// that is used to schedule the task that executes the end method.</param>
  802. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  803. /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  804. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  805. /// <paramref name="asyncResult"/> argument is null.</exception>
  806. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  807. /// <paramref name="endMethod"/> argument is null.</exception>
  808. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  809. /// <paramref name="scheduler"/> argument is null.</exception>
  810. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  811. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  812. /// value.</exception>
  813. /// <returns>A <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the asynchronous
  814. /// operation.</returns>
  815. public Task FromAsync(
  816. IAsyncResult asyncResult,
  817. Action<IAsyncResult> endMethod,
  818. TaskCreationOptions creationOptions,
  819. TaskScheduler scheduler)
  820. {
  821. return TaskFactory<VoidTaskResult>.FromAsyncImpl(asyncResult, null, endMethod, creationOptions, scheduler);
  822. }
  823. /// <summary>
  824. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  825. /// and end methods that conform to the Asynchronous Programming Model pattern.
  826. /// </summary>
  827. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  828. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  829. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  830. /// delegate.</param>
  831. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  832. /// <paramref name="beginMethod"/> argument is null.</exception>
  833. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  834. /// <paramref name="endMethod"/> argument is null.</exception>
  835. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  836. /// asynchronous operation.</returns>
  837. /// <remarks>
  838. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  839. /// </remarks>
  840. public Task FromAsync(
  841. Func<AsyncCallback, object, IAsyncResult> beginMethod,
  842. Action<IAsyncResult> endMethod,
  843. object state)
  844. {
  845. return FromAsync(beginMethod, endMethod, state, m_defaultCreationOptions);
  846. }
  847. /// <summary>
  848. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  849. /// and end methods that conform to the Asynchronous Programming Model pattern.
  850. /// </summary>
  851. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  852. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  853. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  854. /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  855. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  856. /// delegate.</param>
  857. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  858. /// <paramref name="beginMethod"/> argument is null.</exception>
  859. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  860. /// <paramref name="endMethod"/> argument is null.</exception>
  861. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  862. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  863. /// value.</exception>
  864. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  865. /// asynchronous operation.</returns>
  866. /// <remarks>
  867. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  868. /// </remarks>
  869. public Task FromAsync(
  870. Func<AsyncCallback, object, IAsyncResult> beginMethod,
  871. Action<IAsyncResult> endMethod, object state, TaskCreationOptions creationOptions)
  872. {
  873. return TaskFactory<VoidTaskResult>.FromAsyncImpl(beginMethod, null, endMethod, state, creationOptions);
  874. }
  875. /// <summary>
  876. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  877. /// and end methods that conform to the Asynchronous Programming Model pattern.
  878. /// </summary>
  879. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  880. /// name="beginMethod"/>
  881. /// delegate.</typeparam>
  882. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  883. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  884. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  885. /// delegate.</param>
  886. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  887. /// delegate.</param>
  888. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  889. /// <paramref name="beginMethod"/> argument is null.</exception>
  890. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  891. /// <paramref name="endMethod"/> argument is null.</exception>
  892. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  893. /// asynchronous operation.</returns>
  894. /// <remarks>
  895. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  896. /// </remarks>
  897. public Task FromAsync<TArg1>(
  898. Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
  899. Action<IAsyncResult> endMethod,
  900. TArg1 arg1,
  901. object state)
  902. {
  903. return FromAsync(beginMethod, endMethod, arg1, state, m_defaultCreationOptions);
  904. }
  905. /// <summary>
  906. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  907. /// and end methods that conform to the Asynchronous Programming Model pattern.
  908. /// </summary>
  909. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  910. /// name="beginMethod"/>
  911. /// delegate.</typeparam>
  912. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  913. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  914. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  915. /// delegate.</param>
  916. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  917. /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  918. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  919. /// delegate.</param>
  920. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  921. /// <paramref name="beginMethod"/> argument is null.</exception>
  922. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  923. /// <paramref name="endMethod"/> argument is null.</exception>
  924. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  925. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  926. /// value.</exception>
  927. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  928. /// asynchronous operation.</returns>
  929. /// <remarks>
  930. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  931. /// </remarks>
  932. public Task FromAsync<TArg1>(
  933. Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
  934. Action<IAsyncResult> endMethod,
  935. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  936. {
  937. return TaskFactory<VoidTaskResult>.FromAsyncImpl(beginMethod, null, endMethod, arg1, state, creationOptions);
  938. }
  939. /// <summary>
  940. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  941. /// and end methods that conform to the Asynchronous Programming Model pattern.
  942. /// </summary>
  943. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  944. /// name="beginMethod"/>
  945. /// delegate.</typeparam>
  946. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  947. /// delegate.</typeparam>
  948. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  949. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  950. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  951. /// delegate.</param>
  952. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  953. /// delegate.</param>
  954. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  955. /// delegate.</param>
  956. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  957. /// <paramref name="beginMethod"/> argument is null.</exception>
  958. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  959. /// <paramref name="endMethod"/> argument is null.</exception>
  960. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  961. /// asynchronous operation.</returns>
  962. /// <remarks>
  963. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  964. /// </remarks>
  965. public Task FromAsync<TArg1, TArg2>(
  966. Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  967. Action<IAsyncResult> endMethod,
  968. TArg1 arg1, TArg2 arg2, object state)
  969. {
  970. return FromAsync(beginMethod, endMethod, arg1, arg2, state, m_defaultCreationOptions);
  971. }
  972. /// <summary>
  973. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  974. /// and end methods that conform to the Asynchronous Programming Model pattern.
  975. /// </summary>
  976. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  977. /// name="beginMethod"/>
  978. /// delegate.</typeparam>
  979. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  980. /// delegate.</typeparam>
  981. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  982. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  983. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  984. /// delegate.</param>
  985. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  986. /// delegate.</param>
  987. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  988. /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  989. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  990. /// delegate.</param>
  991. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  992. /// <paramref name="beginMethod"/> argument is null.</exception>
  993. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  994. /// <paramref name="endMethod"/> argument is null.</exception>
  995. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  996. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  997. /// value.</exception>
  998. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  999. /// asynchronous operation.</returns>
  1000. /// <remarks>
  1001. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1002. /// </remarks>
  1003. public Task FromAsync<TArg1, TArg2>(
  1004. Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  1005. Action<IAsyncResult> endMethod,
  1006. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  1007. {
  1008. return TaskFactory<VoidTaskResult>.FromAsyncImpl(beginMethod, null, endMethod, arg1, arg2, state, creationOptions);
  1009. }
  1010. /// <summary>
  1011. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  1012. /// and end methods that conform to the Asynchronous Programming Model pattern.
  1013. /// </summary>
  1014. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1015. /// name="beginMethod"/>
  1016. /// delegate.</typeparam>
  1017. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  1018. /// delegate.</typeparam>
  1019. /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
  1020. /// delegate.</typeparam>
  1021. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1022. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1023. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1024. /// delegate.</param>
  1025. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  1026. /// delegate.</param>
  1027. /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
  1028. /// delegate.</param>
  1029. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1030. /// delegate.</param>
  1031. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1032. /// <paramref name="beginMethod"/> argument is null.</exception>
  1033. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1034. /// <paramref name="endMethod"/> argument is null.</exception>
  1035. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  1036. /// asynchronous operation.</returns>
  1037. /// <remarks>
  1038. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1039. /// </remarks>
  1040. public Task FromAsync<TArg1, TArg2, TArg3>(
  1041. Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
  1042. Action<IAsyncResult> endMethod,
  1043. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  1044. {
  1045. return FromAsync(beginMethod, endMethod, arg1, arg2, arg3, state, m_defaultCreationOptions);
  1046. }
  1047. /// <summary>
  1048. /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
  1049. /// and end methods that conform to the Asynchronous Programming Model pattern.
  1050. /// </summary>
  1051. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1052. /// name="beginMethod"/>
  1053. /// delegate.</typeparam>
  1054. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  1055. /// delegate.</typeparam>
  1056. /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
  1057. /// delegate.</typeparam>
  1058. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1059. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1060. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1061. /// delegate.</param>
  1062. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  1063. /// delegate.</param>
  1064. /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
  1065. /// delegate.</param>
  1066. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  1067. /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  1068. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1069. /// delegate.</param>
  1070. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1071. /// <paramref name="beginMethod"/> argument is null.</exception>
  1072. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1073. /// <paramref name="endMethod"/> argument is null.</exception>
  1074. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1075. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  1076. /// value.</exception>
  1077. /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
  1078. /// asynchronous operation.</returns>
  1079. /// <remarks>
  1080. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1081. /// </remarks>
  1082. public Task FromAsync<TArg1, TArg2, TArg3>(
  1083. Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
  1084. Action<IAsyncResult> endMethod,
  1085. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  1086. {
  1087. return TaskFactory<VoidTaskResult>.FromAsyncImpl<TArg1, TArg2, TArg3>(beginMethod, null, endMethod, arg1, arg2, arg3, state, creationOptions);
  1088. }
  1089. //
  1090. // Additional FromAsync() overloads used for inferencing convenience
  1091. //
  1092. /// <summary>
  1093. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
  1094. /// method function when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
  1095. /// </summary>
  1096. /// <typeparam name="TResult">The type of the result available through the
  1097. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1098. /// </typeparam>
  1099. /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
  1100. /// <paramref name="endMethod"/>.</param>
  1101. /// <param name="endMethod">The function delegate that processes the completed <paramref
  1102. /// name="asyncResult"/>.</param>
  1103. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1104. /// <paramref name="asyncResult"/> argument is null.</exception>
  1105. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1106. /// <paramref name="endMethod"/> argument is null.</exception>
  1107. /// <returns>A <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents the
  1108. /// asynchronous operation.</returns>
  1109. public Task<TResult> FromAsync<TResult>(
  1110. IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  1111. {
  1112. return TaskFactory<TResult>.FromAsyncImpl(asyncResult, endMethod, null, m_defaultCreationOptions, DefaultScheduler);
  1113. }
  1114. /// <summary>
  1115. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
  1116. /// method function when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
  1117. /// </summary>
  1118. /// <typeparam name="TResult">The type of the result available through the
  1119. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1120. /// </typeparam>
  1121. /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
  1122. /// <paramref name="endMethod"/>.</param>
  1123. /// <param name="endMethod">The function delegate that processes the completed <paramref
  1124. /// name="asyncResult"/>.</param>
  1125. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  1126. /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1127. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1128. /// <paramref name="asyncResult"/> argument is null.</exception>
  1129. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1130. /// <paramref name="endMethod"/> argument is null.</exception>
  1131. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1132. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  1133. /// value.</exception>
  1134. /// <returns>A <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents the
  1135. /// asynchronous operation.</returns>
  1136. public Task<TResult> FromAsync<TResult>(
  1137. IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
  1138. {
  1139. return TaskFactory<TResult>.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, DefaultScheduler);
  1140. }
  1141. /// <summary>
  1142. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
  1143. /// method function when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
  1144. /// </summary>
  1145. /// <typeparam name="TResult">The type of the result available through the
  1146. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1147. /// </typeparam>
  1148. /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
  1149. /// <paramref name="endMethod"/>.</param>
  1150. /// <param name="endMethod">The function delegate that processes the completed <paramref
  1151. /// name="asyncResult"/>.</param>
  1152. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  1153. /// that is used to schedule the task that executes the end method.</param>
  1154. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  1155. /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1156. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1157. /// <paramref name="asyncResult"/> argument is null.</exception>
  1158. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1159. /// <paramref name="endMethod"/> argument is null.</exception>
  1160. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1161. /// <paramref name="scheduler"/> argument is null.</exception>
  1162. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1163. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  1164. /// value.</exception>
  1165. /// <returns>A <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents the
  1166. /// asynchronous operation.</returns>
  1167. public Task<TResult> FromAsync<TResult>(
  1168. IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  1169. {
  1170. return TaskFactory<TResult>.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, scheduler);
  1171. }
  1172. /// <summary>
  1173. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1174. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1175. /// </summary>
  1176. /// <typeparam name="TResult">The type of the result available through the
  1177. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1178. /// </typeparam>
  1179. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1180. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1181. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1182. /// delegate.</param>
  1183. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1184. /// <paramref name="beginMethod"/> argument is null.</exception>
  1185. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1186. /// <paramref name="endMethod"/> argument is null.</exception>
  1187. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1188. /// represents the asynchronous operation.</returns>
  1189. /// <remarks>
  1190. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1191. /// </remarks>
  1192. public Task<TResult> FromAsync<TResult>(
  1193. Func<AsyncCallback, object, IAsyncResult> beginMethod,
  1194. Func<IAsyncResult, TResult> endMethod, object state)
  1195. {
  1196. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, state, m_defaultCreationOptions);
  1197. }
  1198. /// <summary>
  1199. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1200. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1201. /// </summary>
  1202. /// <typeparam name="TResult">The type of the result available through the
  1203. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1204. /// </typeparam>
  1205. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1206. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1207. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  1208. /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1209. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1210. /// delegate.</param>
  1211. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1212. /// <paramref name="beginMethod"/> argument is null.</exception>
  1213. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1214. /// <paramref name="endMethod"/> argument is null.</exception>
  1215. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1216. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  1217. /// value.</exception>
  1218. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1219. /// represents the asynchronous operation.</returns>
  1220. /// <remarks>
  1221. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1222. /// </remarks>
  1223. public Task<TResult> FromAsync<TResult>(
  1224. Func<AsyncCallback, object, IAsyncResult> beginMethod,
  1225. Func<IAsyncResult, TResult> endMethod, object state, TaskCreationOptions creationOptions)
  1226. {
  1227. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, state, creationOptions);
  1228. }
  1229. /// <summary>
  1230. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1231. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1232. /// </summary>
  1233. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1234. /// name="beginMethod"/> delegate.</typeparam>
  1235. /// <typeparam name="TResult">The type of the result available through the
  1236. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1237. /// </typeparam>
  1238. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1239. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1240. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1241. /// delegate.</param>
  1242. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1243. /// delegate.</param>
  1244. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1245. /// <paramref name="beginMethod"/> argument is null.</exception>
  1246. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1247. /// <paramref name="endMethod"/> argument is null.</exception>
  1248. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1249. /// represents the asynchronous operation.</returns>
  1250. /// <remarks>
  1251. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1252. /// </remarks>
  1253. public Task<TResult> FromAsync<TArg1, TResult>(
  1254. Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
  1255. Func<IAsyncResult, TResult> endMethod, TArg1 arg1, object state)
  1256. {
  1257. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, state, m_defaultCreationOptions);
  1258. }
  1259. /// <summary>
  1260. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1261. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1262. /// </summary>
  1263. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1264. /// name="beginMethod"/> delegate.</typeparam>
  1265. /// <typeparam name="TResult">The type of the result available through the
  1266. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1267. /// </typeparam>
  1268. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1269. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1270. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1271. /// delegate.</param>
  1272. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  1273. /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1274. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1275. /// delegate.</param>
  1276. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1277. /// <paramref name="beginMethod"/> argument is null.</exception>
  1278. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1279. /// <paramref name="endMethod"/> argument is null.</exception>
  1280. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1281. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  1282. /// value.</exception>
  1283. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1284. /// represents the asynchronous operation.</returns>
  1285. /// <remarks>
  1286. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1287. /// </remarks>
  1288. public Task<TResult> FromAsync<TArg1, TResult>(Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
  1289. Func<IAsyncResult, TResult> endMethod, TArg1 arg1, object state, TaskCreationOptions creationOptions)
  1290. {
  1291. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, state, creationOptions);
  1292. }
  1293. /// <summary>
  1294. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1295. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1296. /// </summary>
  1297. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1298. /// name="beginMethod"/> delegate.</typeparam>
  1299. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  1300. /// delegate.</typeparam>
  1301. /// <typeparam name="TResult">The type of the result available through the
  1302. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1303. /// </typeparam>
  1304. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1305. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1306. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1307. /// delegate.</param>
  1308. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  1309. /// delegate.</param>
  1310. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1311. /// delegate.</param>
  1312. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1313. /// <paramref name="beginMethod"/> argument is null.</exception>
  1314. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1315. /// <paramref name="endMethod"/> argument is null.</exception>
  1316. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1317. /// represents the asynchronous operation.</returns>
  1318. /// <remarks>
  1319. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1320. /// </remarks>
  1321. public Task<TResult> FromAsync<TArg1, TArg2, TResult>(Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  1322. Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, object state)
  1323. {
  1324. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, state, m_defaultCreationOptions);
  1325. }
  1326. /// <summary>
  1327. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1328. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1329. /// </summary>
  1330. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1331. /// name="beginMethod"/> delegate.</typeparam>
  1332. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  1333. /// delegate.</typeparam>
  1334. /// <typeparam name="TResult">The type of the result available through the
  1335. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1336. /// </typeparam>
  1337. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1338. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1339. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1340. /// delegate.</param>
  1341. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  1342. /// delegate.</param>
  1343. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  1344. /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1345. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1346. /// delegate.</param>
  1347. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1348. /// <paramref name="beginMethod"/> argument is null.</exception>
  1349. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1350. /// <paramref name="endMethod"/> argument is null.</exception>
  1351. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1352. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  1353. /// value.</exception>
  1354. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1355. /// represents the asynchronous operation.</returns>
  1356. /// <remarks>
  1357. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1358. /// </remarks>
  1359. public Task<TResult> FromAsync<TArg1, TArg2, TResult>(
  1360. Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  1361. Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  1362. {
  1363. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, state, creationOptions);
  1364. }
  1365. /// <summary>
  1366. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1367. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1368. /// </summary>
  1369. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1370. /// name="beginMethod"/> delegate.</typeparam>
  1371. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  1372. /// delegate.</typeparam>
  1373. /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
  1374. /// delegate.</typeparam>
  1375. /// <typeparam name="TResult">The type of the result available through the
  1376. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1377. /// </typeparam>
  1378. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1379. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1380. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1381. /// delegate.</param>
  1382. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  1383. /// delegate.</param>
  1384. /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
  1385. /// delegate.</param>
  1386. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1387. /// delegate.</param>
  1388. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1389. /// <paramref name="beginMethod"/> argument is null.</exception>
  1390. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1391. /// <paramref name="endMethod"/> argument is null.</exception>
  1392. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1393. /// represents the asynchronous operation.</returns>
  1394. /// <remarks>
  1395. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1396. /// </remarks>
  1397. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult>(
  1398. Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
  1399. Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  1400. {
  1401. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, arg3, state, m_defaultCreationOptions);
  1402. }
  1403. /// <summary>
  1404. /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
  1405. /// begin and end methods that conform to the Asynchronous Programming Model pattern.
  1406. /// </summary>
  1407. /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
  1408. /// name="beginMethod"/> delegate.</typeparam>
  1409. /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
  1410. /// delegate.</typeparam>
  1411. /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
  1412. /// delegate.</typeparam>
  1413. /// <typeparam name="TResult">The type of the result available through the
  1414. /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
  1415. /// </typeparam>
  1416. /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
  1417. /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
  1418. /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
  1419. /// delegate.</param>
  1420. /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
  1421. /// delegate.</param>
  1422. /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
  1423. /// delegate.</param>
  1424. /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
  1425. /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1426. /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
  1427. /// delegate.</param>
  1428. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1429. /// <paramref name="beginMethod"/> argument is null.</exception>
  1430. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1431. /// <paramref name="endMethod"/> argument is null.</exception>
  1432. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1433. /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
  1434. /// value.</exception>
  1435. /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
  1436. /// represents the asynchronous operation.</returns>
  1437. /// <remarks>
  1438. /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
  1439. /// </remarks>
  1440. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult>(
  1441. Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
  1442. Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  1443. {
  1444. return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, arg3, state, creationOptions);
  1445. }
  1446. /// <summary>
  1447. /// Check validity of options passed to FromAsync method
  1448. /// </summary>
  1449. /// <param name="creationOptions">The options to be validated.</param>
  1450. /// <param name="hasBeginMethod">determines type of FromAsync method that called this method</param>
  1451. internal static void CheckFromAsyncOptions(TaskCreationOptions creationOptions, bool hasBeginMethod)
  1452. {
  1453. if (hasBeginMethod)
  1454. {
  1455. // Options detected here cause exceptions in FromAsync methods that take beginMethod as a parameter
  1456. if ((creationOptions & TaskCreationOptions.LongRunning) != 0)
  1457. throw new ArgumentOutOfRangeException(nameof(creationOptions), SR.Task_FromAsync_LongRunning);
  1458. if ((creationOptions & TaskCreationOptions.PreferFairness) != 0)
  1459. throw new ArgumentOutOfRangeException(nameof(creationOptions), SR.Task_FromAsync_PreferFairness);
  1460. }
  1461. // Check for general validity of options
  1462. if ((creationOptions &
  1463. ~(TaskCreationOptions.AttachedToParent |
  1464. TaskCreationOptions.DenyChildAttach |
  1465. TaskCreationOptions.HideScheduler |
  1466. TaskCreationOptions.PreferFairness |
  1467. TaskCreationOptions.LongRunning)) != 0)
  1468. {
  1469. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions);
  1470. }
  1471. }
  1472. //
  1473. // ContinueWhenAll methods
  1474. //
  1475. // A Task<Task[]> that, given an initial collection of N tasks, will complete when
  1476. // it has been invoked N times. This allows us to replace this logic:
  1477. // Task<Task[]> promise = new Task<Task[]>(...);
  1478. // int _count = tasksCopy.Length;
  1479. // Action<Task> completionAction = delegate {if(Interlocked.Decrement(ref _count) == 0) promise.TrySetResult(tasksCopy);
  1480. // for(int i=0; i<_count; i++)
  1481. // tasksCopy[i].AddCompletionAction(completionAction);
  1482. // with this logic:
  1483. // CompletionOnCountdownPromise promise = new CompletionOnCountdownPromise(tasksCopy);
  1484. // for(int i=0; i<tasksCopy.Length; i++) tasksCopy[i].AddCompletionAction(promise);
  1485. // which saves a few allocations.
  1486. //
  1487. // Used in TaskFactory.CommonCWAllLogic(Task[]), below.
  1488. private sealed class CompleteOnCountdownPromise : Task<Task[]>, ITaskCompletionAction
  1489. {
  1490. private readonly Task[] _tasks;
  1491. private int _count;
  1492. internal CompleteOnCountdownPromise(Task[] tasksCopy) : base()
  1493. {
  1494. Debug.Assert((tasksCopy != null) && (tasksCopy.Length > 0), "Expected non-null task array with at least one element in it");
  1495. _tasks = tasksCopy;
  1496. _count = tasksCopy.Length;
  1497. if (DebuggerSupport.LoggingOn)
  1498. DebuggerSupport.TraceOperationCreation(CausalityTraceLevel.Required, this, "TaskFactory.ContinueWhenAll", 0);
  1499. DebuggerSupport.AddToActiveTasks(this);
  1500. }
  1501. public void Invoke(Task completingTask)
  1502. {
  1503. if (DebuggerSupport.LoggingOn)
  1504. DebuggerSupport.TraceOperationRelation(CausalityTraceLevel.Important, this, CausalityRelation.Join);
  1505. if (completingTask.IsWaitNotificationEnabled) this.SetNotificationForWaitCompletion(enabled: true);
  1506. if (Interlocked.Decrement(ref _count) == 0)
  1507. {
  1508. if (DebuggerSupport.LoggingOn)
  1509. DebuggerSupport.TraceOperationCompletion(CausalityTraceLevel.Required, this, AsyncStatus.Completed);
  1510. DebuggerSupport.RemoveFromActiveTasks(this);
  1511. TrySetResult(_tasks);
  1512. }
  1513. Debug.Assert(_count >= 0, "Count should never go below 0");
  1514. }
  1515. public bool InvokeMayRunArbitraryCode { get { return true; } }
  1516. /// <summary>
  1517. /// Returns whether we should notify the debugger of a wait completion. This returns
  1518. /// true iff at least one constituent task has its bit set.
  1519. /// </summary>
  1520. internal override bool ShouldNotifyDebuggerOfWaitCompletion
  1521. {
  1522. get
  1523. {
  1524. return
  1525. base.ShouldNotifyDebuggerOfWaitCompletion &&
  1526. Task.AnyTaskRequiresNotifyDebuggerOfWaitCompletion(_tasks);
  1527. }
  1528. }
  1529. }
  1530. // Performs some logic common to all ContinueWhenAll() overloads
  1531. internal static Task<Task[]> CommonCWAllLogic(Task[] tasksCopy)
  1532. {
  1533. Debug.Assert(tasksCopy != null);
  1534. // Create a promise task to be returned to the user
  1535. CompleteOnCountdownPromise promise = new CompleteOnCountdownPromise(tasksCopy);
  1536. for (int i = 0; i < tasksCopy.Length; i++)
  1537. {
  1538. if (tasksCopy[i].IsCompleted) promise.Invoke(tasksCopy[i]); // Short-circuit the completion action, if possible
  1539. else tasksCopy[i].AddCompletionAction(promise); // simple completion action
  1540. }
  1541. return promise;
  1542. }
  1543. // A Task<Task<T>[]> that, given an initial collection of N tasks, will complete when
  1544. // it has been invoked N times. See comments for non-generic CompleteOnCountdownPromise class.
  1545. //
  1546. // Used in TaskFactory.CommonCWAllLogic<TResult>(Task<TResult>[]), below.
  1547. private sealed class CompleteOnCountdownPromise<T> : Task<Task<T>[]>, ITaskCompletionAction
  1548. {
  1549. private readonly Task<T>[] _tasks;
  1550. private int _count;
  1551. internal CompleteOnCountdownPromise(Task<T>[] tasksCopy) : base()
  1552. {
  1553. Debug.Assert((tasksCopy != null) && (tasksCopy.Length > 0), "Expected non-null task array with at least one element in it");
  1554. _tasks = tasksCopy;
  1555. _count = tasksCopy.Length;
  1556. if (DebuggerSupport.LoggingOn)
  1557. DebuggerSupport.TraceOperationCreation(CausalityTraceLevel.Required, this, "TaskFactory.ContinueWhenAll<>", 0);
  1558. DebuggerSupport.AddToActiveTasks(this);
  1559. }
  1560. public void Invoke(Task completingTask)
  1561. {
  1562. if (DebuggerSupport.LoggingOn)
  1563. DebuggerSupport.TraceOperationRelation(CausalityTraceLevel.Important, this, CausalityRelation.Join);
  1564. if (completingTask.IsWaitNotificationEnabled) this.SetNotificationForWaitCompletion(enabled: true);
  1565. if (Interlocked.Decrement(ref _count) == 0)
  1566. {
  1567. if (DebuggerSupport.LoggingOn)
  1568. DebuggerSupport.TraceOperationCompletion(CausalityTraceLevel.Required, this, AsyncStatus.Completed);
  1569. DebuggerSupport.RemoveFromActiveTasks(this);
  1570. TrySetResult(_tasks);
  1571. }
  1572. Debug.Assert(_count >= 0, "Count should never go below 0");
  1573. }
  1574. public bool InvokeMayRunArbitraryCode { get { return true; } }
  1575. /// <summary>
  1576. /// Returns whether we should notify the debugger of a wait completion. This returns
  1577. /// true iff at least one constituent task has its bit set.
  1578. /// </summary>
  1579. internal override bool ShouldNotifyDebuggerOfWaitCompletion
  1580. {
  1581. get
  1582. {
  1583. return
  1584. base.ShouldNotifyDebuggerOfWaitCompletion &&
  1585. Task.AnyTaskRequiresNotifyDebuggerOfWaitCompletion(_tasks);
  1586. }
  1587. }
  1588. }
  1589. internal static Task<Task<T>[]> CommonCWAllLogic<T>(Task<T>[] tasksCopy)
  1590. {
  1591. Debug.Assert(tasksCopy != null);
  1592. // Create a promise task to be returned to the user
  1593. CompleteOnCountdownPromise<T> promise = new CompleteOnCountdownPromise<T>(tasksCopy);
  1594. for (int i = 0; i < tasksCopy.Length; i++)
  1595. {
  1596. if (tasksCopy[i].IsCompleted) promise.Invoke(tasksCopy[i]); // Short-circuit the completion action, if possible
  1597. else tasksCopy[i].AddCompletionAction(promise); // simple completion action
  1598. }
  1599. return promise;
  1600. }
  1601. /// <summary>
  1602. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1603. /// that will be started upon the completion of a set of provided Tasks.
  1604. /// </summary>
  1605. /// <param name="tasks">The array of tasks from which to continue.</param>
  1606. /// <param name="continuationAction">The action delegate to execute when all tasks in
  1607. /// the <paramref name="tasks"/> array have completed.</param>
  1608. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1609. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1610. /// <paramref name="tasks"/> array is null.</exception>
  1611. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1612. /// <paramref name="continuationAction"/> argument is null.</exception>
  1613. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1614. /// <paramref name="tasks"/> array contains a null value.</exception>
  1615. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1616. /// <paramref name="tasks"/> array is empty.</exception>
  1617. public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction)
  1618. {
  1619. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1620. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  1621. }
  1622. /// <summary>
  1623. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1624. /// that will be started upon the completion of a set of provided Tasks.
  1625. /// </summary>
  1626. /// <param name="tasks">The array of tasks from which to continue.</param>
  1627. /// <param name="continuationAction">The action delegate to execute when all tasks in
  1628. /// the <paramref name="tasks"/> array have completed.</param>
  1629. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1630. /// that will be assigned to the new continuation task.</param>
  1631. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1632. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1633. /// <paramref name="tasks"/> array is null.</exception>
  1634. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1635. /// <paramref name="continuationAction"/> argument is null.</exception>
  1636. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1637. /// <paramref name="tasks"/> array contains a null value.</exception>
  1638. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1639. /// <paramref name="tasks"/> array is empty.</exception>
  1640. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1641. /// has already been disposed.
  1642. /// </exception>
  1643. public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
  1644. {
  1645. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1646. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  1647. }
  1648. /// <summary>
  1649. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1650. /// that will be started upon the completion of a set of provided Tasks.
  1651. /// </summary>
  1652. /// <param name="tasks">The array of tasks from which to continue.</param>
  1653. /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
  1654. /// name="tasks"/> array have completed.</param>
  1655. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  1656. /// TaskContinuationOptions</see> value that controls the behavior of
  1657. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  1658. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1659. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1660. /// <paramref name="tasks"/> array is null.</exception>
  1661. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1662. /// <paramref name="continuationAction"/> argument is null.</exception>
  1663. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1664. /// <paramref name="tasks"/> array contains a null value.</exception>
  1665. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1666. /// <paramref name="tasks"/> array is empty.</exception>
  1667. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1668. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  1669. /// value.</exception>
  1670. /// <remarks>
  1671. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  1672. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  1673. /// will be executed, are illegal with ContinueWhenAll.
  1674. /// </remarks>
  1675. public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction, TaskContinuationOptions continuationOptions)
  1676. {
  1677. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1678. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  1679. }
  1680. /// <summary>
  1681. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1682. /// that will be started upon the completion of a set of provided Tasks.
  1683. /// </summary>
  1684. /// <param name="tasks">The array of tasks from which to continue.</param>
  1685. /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
  1686. /// name="tasks"/> array have completed.</param>
  1687. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1688. /// that will be assigned to the new continuation task.</param>
  1689. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  1690. /// TaskContinuationOptions</see> value that controls the behavior of
  1691. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  1692. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  1693. /// that is used to schedule the created continuation <see
  1694. /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  1695. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1696. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1697. /// <paramref name="tasks"/> array is null.</exception>
  1698. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1699. /// <paramref name="continuationAction"/> argument is null.</exception>
  1700. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1701. /// <paramref name="scheduler"/> argument is null.</exception>
  1702. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1703. /// <paramref name="tasks"/> array contains a null value.</exception>
  1704. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1705. /// <paramref name="tasks"/> array is empty.</exception>
  1706. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1707. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  1708. /// value.</exception>
  1709. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1710. /// has already been disposed.
  1711. /// </exception>
  1712. /// <remarks>
  1713. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  1714. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  1715. /// will be executed, are illegal with ContinueWhenAll.
  1716. /// </remarks>
  1717. public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
  1718. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  1719. {
  1720. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1721. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
  1722. }
  1723. /// <summary>
  1724. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1725. /// that will be started upon the completion of a set of provided Tasks.
  1726. /// </summary>
  1727. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  1728. /// <param name="tasks">The array of tasks from which to continue.</param>
  1729. /// <param name="continuationAction">The action delegate to execute when all tasks in
  1730. /// the <paramref name="tasks"/> array have completed.</param>
  1731. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1732. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1733. /// <paramref name="tasks"/> array is null.</exception>
  1734. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1735. /// <paramref name="continuationAction"/> argument is null.</exception>
  1736. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1737. /// <paramref name="tasks"/> array contains a null value.</exception>
  1738. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1739. /// <paramref name="tasks"/> array is empty.</exception>
  1740. public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction)
  1741. {
  1742. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1743. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  1744. }
  1745. /// <summary>
  1746. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1747. /// that will be started upon the completion of a set of provided Tasks.
  1748. /// </summary>
  1749. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  1750. /// <param name="tasks">The array of tasks from which to continue.</param>
  1751. /// <param name="continuationAction">The action delegate to execute when all tasks in
  1752. /// the <paramref name="tasks"/> array have completed.</param>
  1753. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1754. /// that will be assigned to the new continuation task.</param>
  1755. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1756. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1757. /// <paramref name="tasks"/> array is null.</exception>
  1758. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1759. /// <paramref name="continuationAction"/> argument is null.</exception>
  1760. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1761. /// <paramref name="tasks"/> array contains a null value.</exception>
  1762. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1763. /// <paramref name="tasks"/> array is empty.</exception>
  1764. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1765. /// has already been disposed.
  1766. /// </exception>
  1767. public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  1768. CancellationToken cancellationToken)
  1769. {
  1770. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1771. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  1772. }
  1773. /// <summary>
  1774. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1775. /// that will be started upon the completion of a set of provided Tasks.
  1776. /// </summary>
  1777. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  1778. /// <param name="tasks">The array of tasks from which to continue.</param>
  1779. /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
  1780. /// name="tasks"/> array have completed.</param>
  1781. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  1782. /// TaskContinuationOptions</see> value that controls the behavior of
  1783. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  1784. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1785. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1786. /// <paramref name="tasks"/> array is null.</exception>
  1787. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1788. /// <paramref name="continuationAction"/> argument is null.</exception>
  1789. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1790. /// <paramref name="tasks"/> array contains a null value.</exception>
  1791. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1792. /// <paramref name="tasks"/> array is empty.</exception>
  1793. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1794. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  1795. /// value.</exception>
  1796. /// <remarks>
  1797. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  1798. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  1799. /// will be executed, are illegal with ContinueWhenAll.
  1800. /// </remarks>
  1801. public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  1802. TaskContinuationOptions continuationOptions)
  1803. {
  1804. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1805. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  1806. }
  1807. /// <summary>
  1808. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1809. /// that will be started upon the completion of a set of provided Tasks.
  1810. /// </summary>
  1811. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  1812. /// <param name="tasks">The array of tasks from which to continue.</param>
  1813. /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
  1814. /// name="tasks"/> array have completed.</param>
  1815. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1816. /// that will be assigned to the new continuation task.</param>
  1817. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  1818. /// TaskContinuationOptions</see> value that controls the behavior of
  1819. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  1820. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  1821. /// that is used to schedule the created continuation <see
  1822. /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  1823. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  1824. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1825. /// <paramref name="tasks"/> array is null.</exception>
  1826. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1827. /// <paramref name="continuationAction"/> argument is null.</exception>
  1828. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1829. /// <paramref name="scheduler"/> argument is null.</exception>
  1830. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1831. /// <paramref name="tasks"/> array contains a null value.</exception>
  1832. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1833. /// <paramref name="tasks"/> array is empty.</exception>
  1834. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1835. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  1836. /// value.</exception>
  1837. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1838. /// has already been disposed.
  1839. /// </exception>
  1840. /// <remarks>
  1841. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  1842. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  1843. /// will be executed, are illegal with ContinueWhenAll.
  1844. /// </remarks>
  1845. public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  1846. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  1847. {
  1848. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  1849. return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
  1850. }
  1851. /// <summary>
  1852. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1853. /// that will be started upon the completion of a set of provided Tasks.
  1854. /// </summary>
  1855. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  1856. /// name="continuationFunction"/>
  1857. /// delegate and associated with the created <see
  1858. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  1859. /// <param name="tasks">The array of tasks from which to continue.</param>
  1860. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  1861. /// <paramref name="tasks"/> array have completed.</param>
  1862. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  1863. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1864. /// <paramref name="tasks"/> array is null.</exception>
  1865. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1866. /// <paramref name="continuationFunction"/> argument is null.</exception>
  1867. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1868. /// <paramref name="tasks"/> array contains a null value.</exception>
  1869. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1870. /// <paramref name="tasks"/> array is empty.</exception>
  1871. public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction)
  1872. {
  1873. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  1874. return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  1875. }
  1876. /// <summary>
  1877. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  1878. /// that will be started upon the completion of a set of provided Tasks.
  1879. /// </summary>
  1880. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  1881. /// name="continuationFunction"/>
  1882. /// delegate and associated with the created <see
  1883. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  1884. /// <param name="tasks">The array of tasks from which to continue.</param>
  1885. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  1886. /// <paramref name="tasks"/> array have completed.</param>
  1887. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1888. /// that will be assigned to the new continuation task.</param>
  1889. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  1890. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1891. /// <paramref name="tasks"/> array is null.</exception>
  1892. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1893. /// <paramref name="continuationFunction"/> argument is null.</exception>
  1894. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1895. /// <paramref name="tasks"/> array contains a null value.</exception>
  1896. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1897. /// <paramref name="tasks"/> array is empty.</exception>
  1898. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1899. /// has already been disposed.
  1900. /// </exception>
  1901. public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction, CancellationToken cancellationToken)
  1902. {
  1903. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  1904. return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  1905. }
  1906. /// <summary>
  1907. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  1908. /// that will be started upon the completion of a set of provided Tasks.
  1909. /// </summary>
  1910. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  1911. /// name="continuationFunction"/>
  1912. /// delegate and associated with the created <see
  1913. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  1914. /// <param name="tasks">The array of tasks from which to continue.</param>
  1915. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  1916. /// <paramref name="tasks"/> array have completed.</param>
  1917. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  1918. /// TaskContinuationOptions</see> value that controls the behavior of
  1919. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1920. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  1921. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1922. /// <paramref name="tasks"/> array is null.</exception>
  1923. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1924. /// <paramref name="continuationFunction"/> argument is null.</exception>
  1925. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1926. /// <paramref name="tasks"/> array contains a null value.</exception>
  1927. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1928. /// <paramref name="tasks"/> array is empty.</exception>
  1929. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1930. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  1931. /// value.</exception>
  1932. /// <remarks>
  1933. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  1934. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  1935. /// will be executed, are illegal with ContinueWhenAll.
  1936. /// </remarks>
  1937. public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction, TaskContinuationOptions continuationOptions)
  1938. {
  1939. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  1940. return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  1941. }
  1942. /// <summary>
  1943. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  1944. /// that will be started upon the completion of a set of provided Tasks.
  1945. /// </summary>
  1946. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  1947. /// name="continuationFunction"/>
  1948. /// delegate and associated with the created <see
  1949. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  1950. /// <param name="tasks">The array of tasks from which to continue.</param>
  1951. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  1952. /// <paramref name="tasks"/> array have completed.</param>
  1953. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1954. /// that will be assigned to the new continuation task.</param>
  1955. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  1956. /// TaskContinuationOptions</see> value that controls the behavior of
  1957. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  1958. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  1959. /// that is used to schedule the created continuation <see
  1960. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  1961. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  1962. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1963. /// <paramref name="tasks"/> array is null.</exception>
  1964. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1965. /// <paramref name="continuationFunction"/> argument is null.</exception>
  1966. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  1967. /// <paramref name="scheduler"/> argument is null.</exception>
  1968. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1969. /// <paramref name="tasks"/> array contains a null value.</exception>
  1970. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  1971. /// <paramref name="tasks"/> array is empty.</exception>
  1972. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  1973. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  1974. /// value.</exception>
  1975. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  1976. /// has already been disposed.
  1977. /// </exception>
  1978. /// <remarks>
  1979. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  1980. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  1981. /// will be executed, are illegal with ContinueWhenAll.
  1982. /// </remarks>
  1983. public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction, CancellationToken cancellationToken,
  1984. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  1985. {
  1986. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  1987. return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
  1988. }
  1989. /// <summary>
  1990. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  1991. /// that will be started upon the completion of a set of provided Tasks.
  1992. /// </summary>
  1993. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  1994. /// name="continuationFunction"/>
  1995. /// delegate and associated with the created <see
  1996. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  1997. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  1998. /// <param name="tasks">The array of tasks from which to continue.</param>
  1999. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  2000. /// <paramref name="tasks"/> array have completed.</param>
  2001. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2002. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2003. /// <paramref name="tasks"/> array is null.</exception>
  2004. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2005. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2006. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2007. /// <paramref name="tasks"/> array contains a null value.</exception>
  2008. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2009. /// <paramref name="tasks"/> array is empty.</exception>
  2010. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  2011. {
  2012. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2013. return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2014. }
  2015. /// <summary>
  2016. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2017. /// that will be started upon the completion of a set of provided Tasks.
  2018. /// </summary>
  2019. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2020. /// name="continuationFunction"/>
  2021. /// delegate and associated with the created <see
  2022. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2023. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2024. /// <param name="tasks">The array of tasks from which to continue.</param>
  2025. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  2026. /// <paramref name="tasks"/> array have completed.</param>
  2027. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2028. /// that will be assigned to the new continuation task.</param>
  2029. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2030. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2031. /// <paramref name="tasks"/> array is null.</exception>
  2032. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2033. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2034. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2035. /// <paramref name="tasks"/> array contains a null value.</exception>
  2036. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2037. /// <paramref name="tasks"/> array is empty.</exception>
  2038. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2039. /// has already been disposed.
  2040. /// </exception>
  2041. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  2042. CancellationToken cancellationToken)
  2043. {
  2044. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2045. return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  2046. }
  2047. /// <summary>
  2048. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2049. /// that will be started upon the completion of a set of provided Tasks.
  2050. /// </summary>
  2051. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2052. /// name="continuationFunction"/>
  2053. /// delegate and associated with the created <see
  2054. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2055. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2056. /// <param name="tasks">The array of tasks from which to continue.</param>
  2057. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  2058. /// <paramref name="tasks"/> array have completed.</param>
  2059. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2060. /// TaskContinuationOptions</see> value that controls the behavior of
  2061. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  2062. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2063. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2064. /// <paramref name="tasks"/> array is null.</exception>
  2065. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2066. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2067. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2068. /// <paramref name="tasks"/> array contains a null value.</exception>
  2069. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2070. /// <paramref name="tasks"/> array is empty.</exception>
  2071. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2072. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2073. /// value.</exception>
  2074. /// <remarks>
  2075. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2076. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2077. /// will be executed, are illegal with ContinueWhenAll.
  2078. /// </remarks>
  2079. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  2080. TaskContinuationOptions continuationOptions)
  2081. {
  2082. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2083. return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2084. }
  2085. /// <summary>
  2086. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2087. /// that will be started upon the completion of a set of provided Tasks.
  2088. /// </summary>
  2089. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2090. /// name="continuationFunction"/>
  2091. /// delegate and associated with the created <see
  2092. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2093. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2094. /// <param name="tasks">The array of tasks from which to continue.</param>
  2095. /// <param name="continuationFunction">The function delegate to execute when all tasks in the
  2096. /// <paramref name="tasks"/> array have completed.</param>
  2097. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2098. /// that will be assigned to the new continuation task.</param>
  2099. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2100. /// TaskContinuationOptions</see> value that controls the behavior of
  2101. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  2102. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  2103. /// that is used to schedule the created continuation <see
  2104. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  2105. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2106. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2107. /// <paramref name="tasks"/> array is null.</exception>
  2108. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2109. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2110. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2111. /// <paramref name="scheduler"/> argument is null.</exception>
  2112. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2113. /// <paramref name="tasks"/> array contains a null value.</exception>
  2114. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2115. /// <paramref name="tasks"/> array is empty.</exception>
  2116. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2117. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2118. /// value.</exception>
  2119. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2120. /// has already been disposed.
  2121. /// </exception>
  2122. /// <remarks>
  2123. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2124. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2125. /// will be executed, are illegal with ContinueWhenAll.
  2126. /// </remarks>
  2127. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  2128. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  2129. {
  2130. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2131. return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
  2132. }
  2133. //
  2134. // ContinueWhenAny methods
  2135. //
  2136. // A Task<Task> that will be completed the first time that Invoke is called.
  2137. // It allows us to replace this logic:
  2138. // Task<Task> promise = new Task<Task>(...);
  2139. // Action<Task> completionAction = delegate(Task completingTask) { promise.TrySetResult(completingTask); }
  2140. // for(int i=0; i<tasksCopy.Length; i++) tasksCopy[i].AddCompletionAction(completionAction);
  2141. // with this logic:
  2142. // CompletionOnInvokePromise promise = new CompletionOnInvokePromise(tasksCopy);
  2143. // for(int i=0; i<tasksCopy.Length; i++) tasksCopy[i].AddCompletionAction(promise);
  2144. // which saves a couple of allocations.
  2145. //
  2146. // Used in TaskFactory.CommonCWAnyLogic(), below.
  2147. internal sealed class CompleteOnInvokePromise : Task<Task>, ITaskCompletionAction
  2148. {
  2149. private IList<Task> _tasks; // must track this for cleanup
  2150. private int m_firstTaskAlreadyCompleted;
  2151. public CompleteOnInvokePromise(IList<Task> tasks) : base()
  2152. {
  2153. Debug.Assert(tasks != null, "Expected non-null collection of tasks");
  2154. _tasks = tasks;
  2155. if (DebuggerSupport.LoggingOn)
  2156. DebuggerSupport.TraceOperationCreation(CausalityTraceLevel.Required, this, "TaskFactory.ContinueWhenAny", 0);
  2157. DebuggerSupport.AddToActiveTasks(this);
  2158. }
  2159. public void Invoke(Task completingTask)
  2160. {
  2161. if (m_firstTaskAlreadyCompleted == 0 &&
  2162. Interlocked.Exchange(ref m_firstTaskAlreadyCompleted, 1) == 0)
  2163. {
  2164. // This was the first Task to complete.
  2165. bool success = TrySetResult(completingTask);
  2166. Debug.Assert(success, "Only one task should have gotten to this point, and thus this must be successful.");
  2167. // We need to remove continuations that may be left straggling on other tasks.
  2168. // Otherwise, repeated calls to WhenAny using the same task could leak actions.
  2169. // This may also help to avoided unnecessary invocations of this whenComplete delegate.
  2170. // Note that we may be attempting to remove a continuation from a task that hasn't had it
  2171. // added yet; while there's overhead there, the operation won't hurt anything.
  2172. if (DebuggerSupport.LoggingOn)
  2173. {
  2174. DebuggerSupport.TraceOperationRelation(CausalityTraceLevel.Important, this, CausalityRelation.Choice);
  2175. DebuggerSupport.TraceOperationCompletion(CausalityTraceLevel.Required, this, AsyncStatus.Completed);
  2176. }
  2177. DebuggerSupport.RemoveFromActiveTasks(this);
  2178. var tasks = _tasks;
  2179. int numTasks = tasks.Count;
  2180. for (int i = 0; i < numTasks; i++)
  2181. {
  2182. var task = tasks[i];
  2183. if (task != null && // if an element was erroneously nulled out concurrently, just skip it; worst case is we don't remove a continuation
  2184. !task.IsCompleted)
  2185. task.RemoveContinuation(this);
  2186. }
  2187. _tasks = null;
  2188. }
  2189. }
  2190. public bool InvokeMayRunArbitraryCode { get { return true; } }
  2191. }
  2192. // Common ContinueWhenAny logic
  2193. // If the tasks list is not an array, it must be an internal defensive copy so that
  2194. // we don't need to be concerned about concurrent modifications to the list. If the task list
  2195. // is an array, it should be a defensive copy if this functionality is being used
  2196. // asynchronously (e.g. WhenAny) rather than synchronously (e.g. WaitAny).
  2197. internal static Task<Task> CommonCWAnyLogic(IList<Task> tasks)
  2198. {
  2199. Debug.Assert(tasks != null);
  2200. // Create a promise task to be returned to the user.
  2201. // (If this logic ever changes, also update CommonCWAnyLogicCleanup.)
  2202. var promise = new CompleteOnInvokePromise(tasks);
  2203. // At the completion of any of the tasks, complete the promise.
  2204. bool checkArgsOnly = false;
  2205. int numTasks = tasks.Count;
  2206. for (int i = 0; i < numTasks; i++)
  2207. {
  2208. var task = tasks[i];
  2209. if (task == null) throw new ArgumentException(SR.Task_MultiTaskContinuation_NullTask, nameof(tasks));
  2210. if (checkArgsOnly) continue;
  2211. // If the promise has already completed, don't bother with checking any more tasks.
  2212. if (promise.IsCompleted)
  2213. {
  2214. checkArgsOnly = true;
  2215. }
  2216. // If a task has already completed, complete the promise.
  2217. else if (task.IsCompleted)
  2218. {
  2219. promise.Invoke(task);
  2220. checkArgsOnly = true;
  2221. }
  2222. // Otherwise, add the completion action and keep going.
  2223. else
  2224. {
  2225. task.AddCompletionAction(promise);
  2226. if (promise.IsCompleted)
  2227. {
  2228. // One of the previous tasks that already had its continuation registered may have
  2229. // raced to complete with our adding the continuation to this task. The completion
  2230. // routine would have gone through and removed the continuation from all of the tasks
  2231. // with which it was already registered, but if the race causes this continuation to
  2232. // be added after that, it'll never be removed. As such, after adding the continuation,
  2233. // we check to see whether the promise has already completed, and if it has, we try to
  2234. // manually remove the continuation from this task. If it was already removed, it'll be
  2235. // a nop, and if we race to remove it, the synchronization in RemoveContinuation will
  2236. // keep things consistent.
  2237. task.RemoveContinuation(promise);
  2238. }
  2239. }
  2240. }
  2241. return promise;
  2242. }
  2243. /// <summary>
  2244. /// Cleans up the operations performed by CommonCWAnyLogic in a case where
  2245. /// the created continuation task is being discarded.
  2246. /// </summary>
  2247. /// <param name="continuation">The task returned from CommonCWAnyLogic.</param>
  2248. internal static void CommonCWAnyLogicCleanup(Task<Task> continuation)
  2249. {
  2250. // Force cleanup of the promise (e.g. removing continuations from each
  2251. // constituent task), by completing the promise with any value.
  2252. ((CompleteOnInvokePromise)continuation).Invoke(null);
  2253. }
  2254. /// <summary>
  2255. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2256. /// that will be started upon the completion of any Task in the provided set.
  2257. /// </summary>
  2258. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2259. /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
  2260. /// name="tasks"/> array completes.</param>
  2261. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  2262. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2263. /// <paramref name="tasks"/> array is null.</exception>
  2264. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2265. /// <paramref name="continuationAction"/> argument is null.</exception>
  2266. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2267. /// <paramref name="tasks"/> array contains a null value.</exception>
  2268. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2269. /// <paramref name="tasks"/> array is empty.</exception>
  2270. public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction)
  2271. {
  2272. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2273. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2274. }
  2275. /// <summary>
  2276. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2277. /// that will be started upon the completion of any Task in the provided set.
  2278. /// </summary>
  2279. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2280. /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
  2281. /// name="tasks"/> array completes.</param>
  2282. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2283. /// that will be assigned to the new continuation task.</param>
  2284. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  2285. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2286. /// <paramref name="tasks"/> array is null.</exception>
  2287. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2288. /// <paramref name="continuationAction"/> argument is null.</exception>
  2289. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2290. /// <paramref name="tasks"/> array contains a null value.</exception>
  2291. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2292. /// <paramref name="tasks"/> array is empty.</exception>
  2293. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2294. /// has already been disposed.
  2295. /// </exception>
  2296. public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
  2297. {
  2298. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2299. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  2300. }
  2301. /// <summary>
  2302. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2303. /// that will be started upon the completion of any Task in the provided set.
  2304. /// </summary>
  2305. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2306. /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
  2307. /// name="tasks"/> array completes.</param>
  2308. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2309. /// TaskContinuationOptions</see> value that controls the behavior of
  2310. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  2311. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  2312. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2313. /// <paramref name="tasks"/> array is null.</exception>
  2314. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2315. /// <paramref name="continuationAction"/> argument is null.</exception>
  2316. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2317. /// <paramref name="tasks"/> array contains a null value.</exception>
  2318. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2319. /// <paramref name="tasks"/> array is empty.</exception>
  2320. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2321. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2322. /// value.</exception>
  2323. /// <remarks>
  2324. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2325. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2326. /// will be executed, are illegal with ContinueWhenAny.
  2327. /// </remarks>
  2328. public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  2329. {
  2330. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2331. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2332. }
  2333. /// <summary>
  2334. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2335. /// that will be started upon the completion of any Task in the provided set.
  2336. /// </summary>
  2337. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2338. /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
  2339. /// name="tasks"/> array completes.</param>
  2340. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2341. /// that will be assigned to the new continuation task.</param>
  2342. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2343. /// TaskContinuationOptions</see> value that controls the behavior of
  2344. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  2345. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  2346. /// that is used to schedule the created continuation <see
  2347. /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  2348. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
  2349. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2350. /// <paramref name="tasks"/> array is null.</exception>
  2351. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2352. /// <paramref name="continuationAction"/> argument is null.</exception>
  2353. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2354. /// <paramref name="scheduler"/> argument is null.</exception>
  2355. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2356. /// <paramref name="tasks"/> array contains a null value.</exception>
  2357. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2358. /// <paramref name="tasks"/> array is empty.</exception>
  2359. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2360. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2361. /// value.</exception>
  2362. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2363. /// has already been disposed.
  2364. /// </exception>
  2365. /// <remarks>
  2366. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2367. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2368. /// will be executed, are illegal with ContinueWhenAny.
  2369. /// </remarks>
  2370. public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken,
  2371. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  2372. {
  2373. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2374. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
  2375. }
  2376. /// <summary>
  2377. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2378. /// that will be started upon the completion of any Task in the provided set.
  2379. /// </summary>
  2380. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2381. /// name="continuationFunction"/>
  2382. /// delegate and associated with the created <see
  2383. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2384. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2385. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2386. /// <paramref name="tasks"/> array completes.</param>
  2387. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2388. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2389. /// <paramref name="tasks"/> array is null.</exception>
  2390. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2391. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2392. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2393. /// <paramref name="tasks"/> array contains a null value.</exception>
  2394. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2395. /// <paramref name="tasks"/> array is empty.</exception>
  2396. public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction)
  2397. {
  2398. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2399. return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2400. }
  2401. /// <summary>
  2402. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2403. /// that will be started upon the completion of any Task in the provided set.
  2404. /// </summary>
  2405. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2406. /// name="continuationFunction"/>
  2407. /// delegate and associated with the created <see
  2408. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2409. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2410. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2411. /// <paramref name="tasks"/> array completes.</param>
  2412. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2413. /// that will be assigned to the new continuation task.</param>
  2414. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2415. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2416. /// <paramref name="tasks"/> array is null.</exception>
  2417. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2418. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2419. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2420. /// <paramref name="tasks"/> array contains a null value.</exception>
  2421. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2422. /// <paramref name="tasks"/> array is empty.</exception>
  2423. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2424. /// has already been disposed.
  2425. /// </exception>
  2426. public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction, CancellationToken cancellationToken)
  2427. {
  2428. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2429. return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  2430. }
  2431. /// <summary>
  2432. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2433. /// that will be started upon the completion of any Task in the provided set.
  2434. /// </summary>
  2435. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2436. /// name="continuationFunction"/>
  2437. /// delegate and associated with the created <see
  2438. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2439. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2440. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2441. /// <paramref name="tasks"/> array completes.</param>
  2442. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2443. /// TaskContinuationOptions</see> value that controls the behavior of
  2444. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  2445. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2446. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2447. /// <paramref name="tasks"/> array is null.</exception>
  2448. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2449. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2450. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2451. /// <paramref name="tasks"/> array contains a null value.</exception>
  2452. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2453. /// <paramref name="tasks"/> array is empty.</exception>
  2454. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2455. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2456. /// value.</exception>
  2457. /// <remarks>
  2458. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2459. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2460. /// will be executed, are illegal with ContinueWhenAny.
  2461. /// </remarks>
  2462. public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction, TaskContinuationOptions continuationOptions)
  2463. {
  2464. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2465. return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2466. }
  2467. /// <summary>
  2468. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2469. /// that will be started upon the completion of any Task in the provided set.
  2470. /// </summary>
  2471. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2472. /// name="continuationFunction"/>
  2473. /// delegate and associated with the created <see
  2474. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2475. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2476. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2477. /// <paramref name="tasks"/> array completes.</param>
  2478. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2479. /// that will be assigned to the new continuation task.</param>
  2480. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2481. /// TaskContinuationOptions</see> value that controls the behavior of
  2482. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  2483. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  2484. /// that is used to schedule the created continuation <see
  2485. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  2486. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2487. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2488. /// <paramref name="tasks"/> array is null.</exception>
  2489. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2490. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2491. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2492. /// <paramref name="scheduler"/> argument is null.</exception>
  2493. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2494. /// <paramref name="tasks"/> array contains a null value.</exception>
  2495. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2496. /// <paramref name="tasks"/> array is empty.</exception>
  2497. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2498. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2499. /// value.</exception>
  2500. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2501. /// has already been disposed.
  2502. /// </exception>
  2503. /// <remarks>
  2504. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2505. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2506. /// will be executed, are illegal with ContinueWhenAny.
  2507. /// </remarks>
  2508. public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction, CancellationToken cancellationToken,
  2509. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  2510. {
  2511. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2512. return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
  2513. }
  2514. /// <summary>
  2515. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2516. /// that will be started upon the completion of any Task in the provided set.
  2517. /// </summary>
  2518. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2519. /// name="continuationFunction"/>
  2520. /// delegate and associated with the created <see
  2521. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2522. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2523. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2524. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2525. /// <paramref name="tasks"/> array completes.</param>
  2526. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2527. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2528. /// <paramref name="tasks"/> array is null.</exception>
  2529. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2530. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2531. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2532. /// <paramref name="tasks"/> array contains a null value.</exception>
  2533. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2534. /// <paramref name="tasks"/> array is empty.</exception>
  2535. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction)
  2536. {
  2537. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2538. return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2539. }
  2540. /// <summary>
  2541. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2542. /// that will be started upon the completion of any Task in the provided set.
  2543. /// </summary>
  2544. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2545. /// name="continuationFunction"/>
  2546. /// delegate and associated with the created <see
  2547. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2548. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2549. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2550. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2551. /// <paramref name="tasks"/> array completes.</param>
  2552. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2553. /// that will be assigned to the new continuation task.</param>
  2554. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2555. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2556. /// <paramref name="tasks"/> array is null.</exception>
  2557. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2558. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2559. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2560. /// <paramref name="tasks"/> array contains a null value.</exception>
  2561. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2562. /// <paramref name="tasks"/> array is empty.</exception>
  2563. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2564. /// has already been disposed.
  2565. /// </exception>
  2566. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction,
  2567. CancellationToken cancellationToken)
  2568. {
  2569. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2570. return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  2571. }
  2572. /// <summary>
  2573. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2574. /// that will be started upon the completion of any Task in the provided set.
  2575. /// </summary>
  2576. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2577. /// name="continuationFunction"/>
  2578. /// delegate and associated with the created <see
  2579. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2580. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2581. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2582. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2583. /// <paramref name="tasks"/> array completes.</param>
  2584. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2585. /// TaskContinuationOptions</see> value that controls the behavior of
  2586. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  2587. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2588. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2589. /// <paramref name="tasks"/> array is null.</exception>
  2590. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2591. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2592. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2593. /// <paramref name="tasks"/> array contains a null value.</exception>
  2594. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2595. /// <paramref name="tasks"/> array is empty.</exception>
  2596. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2597. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2598. /// value.</exception>
  2599. /// <remarks>
  2600. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2601. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2602. /// will be executed, are illegal with ContinueWhenAny.
  2603. /// </remarks>
  2604. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction,
  2605. TaskContinuationOptions continuationOptions)
  2606. {
  2607. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2608. return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2609. }
  2610. /// <summary>
  2611. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
  2612. /// that will be started upon the completion of any Task in the provided set.
  2613. /// </summary>
  2614. /// <typeparam name="TResult">The type of the result that is returned by the <paramref
  2615. /// name="continuationFunction"/>
  2616. /// delegate and associated with the created <see
  2617. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
  2618. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2619. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2620. /// <param name="continuationFunction">The function delegate to execute when one task in the
  2621. /// <paramref name="tasks"/> array completes.</param>
  2622. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2623. /// that will be assigned to the new continuation task.</param>
  2624. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2625. /// TaskContinuationOptions</see> value that controls the behavior of
  2626. /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
  2627. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  2628. /// that is used to schedule the created continuation <see
  2629. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  2630. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
  2631. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2632. /// <paramref name="tasks"/> array is null.</exception>
  2633. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2634. /// <paramref name="continuationFunction"/> argument is null.</exception>
  2635. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2636. /// <paramref name="scheduler"/> argument is null.</exception>
  2637. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2638. /// <paramref name="tasks"/> array contains a null value.</exception>
  2639. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2640. /// <paramref name="tasks"/> array is empty.</exception>
  2641. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2642. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2643. /// value.</exception>
  2644. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2645. /// has already been disposed.
  2646. /// </exception>
  2647. /// <remarks>
  2648. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2649. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2650. /// will be executed, are illegal with ContinueWhenAny.
  2651. /// </remarks>
  2652. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction,
  2653. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  2654. {
  2655. if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
  2656. return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
  2657. }
  2658. /// <summary>
  2659. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2660. /// that will be started upon the completion of any Task in the provided set.
  2661. /// </summary>
  2662. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2663. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2664. /// <param name="continuationAction">The action delegate to execute when one task in the
  2665. /// <paramref name="tasks"/> array completes.</param>
  2666. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
  2667. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2668. /// <paramref name="tasks"/> array is null.</exception>
  2669. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2670. /// <paramref name="continuationAction"/> argument is null.</exception>
  2671. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2672. /// <paramref name="tasks"/> array contains a null value.</exception>
  2673. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2674. /// <paramref name="tasks"/> array is empty.</exception>
  2675. public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction)
  2676. {
  2677. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2678. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2679. }
  2680. /// <summary>
  2681. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2682. /// that will be started upon the completion of any Task in the provided set.
  2683. /// </summary>
  2684. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2685. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2686. /// <param name="continuationAction">The action delegate to execute when one task in the
  2687. /// <paramref name="tasks"/> array completes.</param>
  2688. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2689. /// that will be assigned to the new continuation task.</param>
  2690. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
  2691. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2692. /// <paramref name="tasks"/> array is null.</exception>
  2693. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2694. /// <paramref name="continuationAction"/> argument is null.</exception>
  2695. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2696. /// <paramref name="tasks"/> array contains a null value.</exception>
  2697. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2698. /// <paramref name="tasks"/> array is empty.</exception>
  2699. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2700. /// has already been disposed.
  2701. /// </exception>
  2702. public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
  2703. CancellationToken cancellationToken)
  2704. {
  2705. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2706. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
  2707. }
  2708. /// <summary>
  2709. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2710. /// that will be started upon the completion of any Task in the provided set.
  2711. /// </summary>
  2712. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2713. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2714. /// <param name="continuationAction">The action delegate to execute when one task in the
  2715. /// <paramref name="tasks"/> array completes.</param>
  2716. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2717. /// TaskContinuationOptions</see> value that controls the behavior of
  2718. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  2719. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
  2720. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2721. /// <paramref name="tasks"/> array is null.</exception>
  2722. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2723. /// <paramref name="continuationAction"/> argument is null.</exception>
  2724. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2725. /// <paramref name="tasks"/> array contains a null value.</exception>
  2726. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2727. /// <paramref name="tasks"/> array is empty.</exception>
  2728. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2729. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2730. /// value.</exception>
  2731. /// <remarks>
  2732. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2733. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2734. /// will be executed, are illegal with ContinueWhenAny.
  2735. /// </remarks>
  2736. public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
  2737. TaskContinuationOptions continuationOptions)
  2738. {
  2739. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2740. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
  2741. }
  2742. /// <summary>
  2743. /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
  2744. /// that will be started upon the completion of any Task in the provided set.
  2745. /// </summary>
  2746. /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
  2747. /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
  2748. /// <param name="continuationAction">The action delegate to execute when one task in the
  2749. /// <paramref name="tasks"/> array completes.</param>
  2750. /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2751. /// that will be assigned to the new continuation task.</param>
  2752. /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
  2753. /// TaskContinuationOptions</see> value that controls the behavior of
  2754. /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
  2755. /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
  2756. /// that is used to schedule the created continuation <see
  2757. /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
  2758. /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
  2759. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2760. /// <paramref name="tasks"/> array is null.</exception>
  2761. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2762. /// <paramref name="continuationAction"/> argument is null.</exception>
  2763. /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
  2764. /// <paramref name="scheduler"/> argument is null.</exception>
  2765. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2766. /// <paramref name="tasks"/> array contains a null value.</exception>
  2767. /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
  2768. /// <paramref name="tasks"/> array is empty.</exception>
  2769. /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
  2770. /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
  2771. /// value.</exception>
  2772. /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
  2773. /// has already been disposed.
  2774. /// </exception>
  2775. /// <remarks>
  2776. /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
  2777. /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
  2778. /// will be executed, are illegal with ContinueWhenAny.
  2779. /// </remarks>
  2780. public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
  2781. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  2782. {
  2783. if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
  2784. return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
  2785. }
  2786. // Check task array and return a defensive copy.
  2787. // Used with ContinueWhenAll()/ContinueWhenAny().
  2788. internal static Task[] CheckMultiContinuationTasksAndCopy(Task[] tasks)
  2789. {
  2790. if (tasks == null)
  2791. throw new ArgumentNullException(nameof(tasks));
  2792. if (tasks.Length == 0)
  2793. throw new ArgumentException(SR.Task_MultiTaskContinuation_EmptyTaskList, nameof(tasks));
  2794. Task[] tasksCopy = new Task[tasks.Length];
  2795. for (int i = 0; i < tasks.Length; i++)
  2796. {
  2797. tasksCopy[i] = tasks[i];
  2798. if (tasksCopy[i] == null)
  2799. throw new ArgumentException(SR.Task_MultiTaskContinuation_NullTask, nameof(tasks));
  2800. }
  2801. return tasksCopy;
  2802. }
  2803. internal static Task<TResult>[] CheckMultiContinuationTasksAndCopy<TResult>(Task<TResult>[] tasks)
  2804. {
  2805. if (tasks == null)
  2806. throw new ArgumentNullException(nameof(tasks));
  2807. if (tasks.Length == 0)
  2808. throw new ArgumentException(SR.Task_MultiTaskContinuation_EmptyTaskList, nameof(tasks));
  2809. Task<TResult>[] tasksCopy = new Task<TResult>[tasks.Length];
  2810. for (int i = 0; i < tasks.Length; i++)
  2811. {
  2812. tasksCopy[i] = tasks[i];
  2813. if (tasksCopy[i] == null)
  2814. throw new ArgumentException(SR.Task_MultiTaskContinuation_NullTask, nameof(tasks));
  2815. }
  2816. return tasksCopy;
  2817. }
  2818. // Throw an exception if "options" argument specifies illegal options
  2819. internal static void CheckMultiTaskContinuationOptions(TaskContinuationOptions continuationOptions)
  2820. {
  2821. // Construct a mask to check for illegal options
  2822. const TaskContinuationOptions NotOnAny = TaskContinuationOptions.NotOnCanceled |
  2823. TaskContinuationOptions.NotOnFaulted |
  2824. TaskContinuationOptions.NotOnRanToCompletion;
  2825. // Check that LongRunning and ExecuteSynchronously are not specified together
  2826. const TaskContinuationOptions illegalMask = TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning;
  2827. if ((continuationOptions & illegalMask) == illegalMask)
  2828. {
  2829. throw new ArgumentOutOfRangeException(nameof(continuationOptions), SR.Task_ContinueWith_ESandLR);
  2830. }
  2831. // Check that no nonsensical options are specified.
  2832. if ((continuationOptions & ~(
  2833. TaskContinuationOptions.LongRunning |
  2834. TaskContinuationOptions.PreferFairness |
  2835. TaskContinuationOptions.AttachedToParent |
  2836. TaskContinuationOptions.DenyChildAttach |
  2837. TaskContinuationOptions.HideScheduler |
  2838. TaskContinuationOptions.LazyCancellation |
  2839. NotOnAny |
  2840. TaskContinuationOptions.ExecuteSynchronously)) != 0)
  2841. {
  2842. throw new ArgumentOutOfRangeException(nameof(continuationOptions));
  2843. }
  2844. // Check that no "fire" options are specified.
  2845. if ((continuationOptions & NotOnAny) != 0)
  2846. throw new ArgumentOutOfRangeException(nameof(continuationOptions), SR.Task_MultiTaskContinuation_FireOptions);
  2847. }
  2848. }
  2849. }