| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
- //
- //
- //
- // There are a plethora of common patterns for which Tasks are created. TaskFactory encodes
- // these patterns into helper methods. These helpers also pick up default configuration settings
- // applicable to the entire factory and configurable through its constructors.
- //
- // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- using System;
- using System.Collections.Generic;
- using System.Security;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using System.Diagnostics;
- using AsyncStatus = Internal.Runtime.Augments.AsyncStatus;
- using CausalityRelation = Internal.Runtime.Augments.CausalityRelation;
- using CausalitySource = Internal.Runtime.Augments.CausalitySource;
- using CausalityTraceLevel = Internal.Runtime.Augments.CausalityTraceLevel;
- using CausalitySynchronousWork = Internal.Runtime.Augments.CausalitySynchronousWork;
- namespace System.Threading.Tasks
- {
- /// <summary>
- /// Provides support for creating and scheduling
- /// <see cref="T:System.Threading.Tasks.Task">Tasks</see>.
- /// </summary>
- /// <remarks>
- /// <para>
- /// There are many common patterns for which tasks are relevant. The <see cref="TaskFactory"/>
- /// class encodes some of these patterns into methods that pick up default settings, which are
- /// configurable through its constructors.
- /// </para>
- /// <para>
- /// A default instance of <see cref="TaskFactory"/> is available through the
- /// <see cref="System.Threading.Tasks.Task.Factory">Task.Factory</see> property.
- /// </para>
- /// </remarks>
- public class TaskFactory
- {
- // member variables
- private readonly CancellationToken m_defaultCancellationToken;
- private readonly TaskScheduler m_defaultScheduler;
- private readonly TaskCreationOptions m_defaultCreationOptions;
- private readonly TaskContinuationOptions m_defaultContinuationOptions;
- private TaskScheduler DefaultScheduler => m_defaultScheduler ?? TaskScheduler.Current;
- // sister method to above property -- avoids a TLS lookup
- private TaskScheduler GetDefaultScheduler(Task currTask)
- {
- return
- m_defaultScheduler ??
- (currTask != null && (currTask.CreationOptions & TaskCreationOptions.HideScheduler) == 0 ? currTask.ExecutingTaskScheduler :
- TaskScheduler.Default);
- }
- /* Constructors */
- // ctor parameters provide defaults for the factory, which can be overridden by options provided to
- // specific calls on the factory
- /// <summary>
- /// Initializes a <see cref="TaskFactory"/> instance with the default configuration.
- /// </summary>
- /// <remarks>
- /// This constructor creates a <see cref="TaskFactory"/> instance with a default configuration. The
- /// <see cref="TaskCreationOptions"/> property is initialized to
- /// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
- /// <see cref="TaskContinuationOptions"/> property is initialized to <see
- /// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
- /// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
- /// initialized to the current scheduler (see <see
- /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
- /// </remarks>
- public TaskFactory()
- : this(default, TaskCreationOptions.None, TaskContinuationOptions.None, null)
- {
- }
- /// <summary>
- /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
- /// </summary>
- /// <param name="cancellationToken">The default <see cref="CancellationToken"/> that will be assigned
- /// to tasks created by this <see cref="TaskFactory"/> unless another CancellationToken is explicitly specified
- /// while calling the factory methods.</param>
- /// <remarks>
- /// This constructor creates a <see cref="TaskFactory"/> instance with a default configuration. The
- /// <see cref="TaskCreationOptions"/> property is initialized to
- /// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
- /// <see cref="TaskContinuationOptions"/> property is initialized to <see
- /// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
- /// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
- /// initialized to the current scheduler (see <see
- /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
- /// </remarks>
- public TaskFactory(CancellationToken cancellationToken)
- : this(cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
- {
- }
- /// <summary>
- /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
- /// </summary>
- /// <param name="scheduler">
- /// The <see cref="System.Threading.Tasks.TaskScheduler">
- /// TaskScheduler</see> to use to schedule any tasks created with this TaskFactory. A null value
- /// indicates that the current TaskScheduler should be used.
- /// </param>
- /// <remarks>
- /// With this constructor, the
- /// <see cref="TaskCreationOptions"/> property is initialized to
- /// <see cref="System.Threading.Tasks.TaskCreationOptions.None">TaskCreationOptions.None</see>, the
- /// <see cref="TaskContinuationOptions"/> property is initialized to <see
- /// cref="System.Threading.Tasks.TaskContinuationOptions.None">TaskContinuationOptions.None</see>,
- /// and the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is
- /// initialized to <paramref name="scheduler"/>, unless it's null, in which case the property is
- /// initialized to the current scheduler (see <see
- /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
- /// </remarks>
- public TaskFactory(TaskScheduler scheduler) // null means to use TaskScheduler.Current
- : this(default, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
- {
- }
- /// <summary>
- /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
- /// </summary>
- /// <param name="creationOptions">
- /// The default <see cref="System.Threading.Tasks.TaskCreationOptions">
- /// TaskCreationOptions</see> to use when creating tasks with this TaskFactory.
- /// </param>
- /// <param name="continuationOptions">
- /// The default <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> to use when creating continuation tasks with this TaskFactory.
- /// </param>
- /// <exception cref="T:System.ArgumentOutOfRangeException">
- /// The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument or the <paramref name="continuationOptions"/>
- /// argument specifies an invalid value.
- /// </exception>
- /// <remarks>
- /// With this constructor, the
- /// <see cref="TaskCreationOptions"/> property is initialized to <paramref name="creationOptions"/>,
- /// the
- /// <see cref="TaskContinuationOptions"/> property is initialized to <paramref
- /// name="continuationOptions"/>, and the <see
- /// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is initialized to the
- /// current scheduler (see <see
- /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
- /// </remarks>
- public TaskFactory(TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
- : this(default, creationOptions, continuationOptions, null)
- {
- }
- /// <summary>
- /// Initializes a <see cref="TaskFactory"/> instance with the specified configuration.
- /// </summary>
- /// <param name="cancellationToken">The default <see cref="CancellationToken"/> that will be assigned
- /// to tasks created by this <see cref="TaskFactory"/> unless another CancellationToken is explicitly specified
- /// while calling the factory methods.</param>
- /// <param name="creationOptions">
- /// The default <see cref="System.Threading.Tasks.TaskCreationOptions">
- /// TaskCreationOptions</see> to use when creating tasks with this TaskFactory.
- /// </param>
- /// <param name="continuationOptions">
- /// The default <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> to use when creating continuation tasks with this TaskFactory.
- /// </param>
- /// <param name="scheduler">
- /// The default <see cref="System.Threading.Tasks.TaskScheduler">
- /// TaskScheduler</see> to use to schedule any Tasks created with this TaskFactory. A null value
- /// indicates that TaskScheduler.Current should be used.
- /// </param>
- /// <exception cref="T:System.ArgumentOutOfRangeException">
- /// The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument or the <paramref name="continuationOptions"/>
- /// argumentspecifies an invalid value.
- /// </exception>
- /// <remarks>
- /// With this constructor, the
- /// <see cref="TaskCreationOptions"/> property is initialized to <paramref name="creationOptions"/>,
- /// the
- /// <see cref="TaskContinuationOptions"/> property is initialized to <paramref
- /// name="continuationOptions"/>, and the <see
- /// cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> property is initialized to
- /// <paramref name="scheduler"/>, unless it's null, in which case the property is initialized to the
- /// current scheduler (see <see
- /// cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>).
- /// </remarks>
- public TaskFactory(CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- CheckMultiTaskContinuationOptions(continuationOptions);
- CheckCreationOptions(creationOptions);
- m_defaultCancellationToken = cancellationToken;
- m_defaultScheduler = scheduler;
- m_defaultCreationOptions = creationOptions;
- m_defaultContinuationOptions = continuationOptions;
- }
- internal static void CheckCreationOptions(TaskCreationOptions creationOptions)
- {
- // Check for validity of options
- if ((creationOptions &
- ~(TaskCreationOptions.AttachedToParent |
- TaskCreationOptions.DenyChildAttach |
- TaskCreationOptions.HideScheduler |
- TaskCreationOptions.LongRunning |
- TaskCreationOptions.PreferFairness |
- TaskCreationOptions.RunContinuationsAsynchronously)) != 0)
- {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions);
- }
- }
- /* Properties */
- /// <summary>
- /// Gets the default <see cref="System.Threading.CancellationToken">CancellationToken</see> of this
- /// TaskFactory.
- /// </summary>
- /// <remarks>
- /// This property returns the default <see cref="CancellationToken"/> that will be assigned to all
- /// tasks created by this factory unless another CancellationToken value is explicitly specified
- /// during the call to the factory methods.
- /// </remarks>
- public CancellationToken CancellationToken { get { return m_defaultCancellationToken; } }
- /// <summary>
- /// Gets the <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> of this
- /// TaskFactory.
- /// </summary>
- /// <remarks>
- /// This property returns the default scheduler for this factory. It will be used to schedule all
- /// tasks unless another scheduler is explicitly specified during calls to this factory's methods.
- /// If null, <see cref="System.Threading.Tasks.TaskScheduler.Current">TaskScheduler.Current</see>
- /// will be used.
- /// </remarks>
- public TaskScheduler Scheduler { get { return m_defaultScheduler; } }
- /// <summary>
- /// Gets the <see cref="System.Threading.Tasks.TaskCreationOptions">TaskCreationOptions
- /// </see> value of this TaskFactory.
- /// </summary>
- /// <remarks>
- /// This property returns the default creation options for this factory. They will be used to create all
- /// tasks unless other options are explicitly specified during calls to this factory's methods.
- /// </remarks>
- public TaskCreationOptions CreationOptions { get { return m_defaultCreationOptions; } }
- /// <summary>
- /// Gets the <see cref="System.Threading.Tasks.TaskCreationOptions">TaskContinuationOptions
- /// </see> value of this TaskFactory.
- /// </summary>
- /// <remarks>
- /// This property returns the default continuation options for this factory. They will be used to create
- /// all continuation tasks unless other options are explicitly specified during calls to this factory's methods.
- /// </remarks>
- public TaskContinuationOptions ContinuationOptions { get { return m_defaultContinuationOptions; } }
- //
- // StartNew methods
- //
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref name="action"/>
- /// argument is null.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors
- /// and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution. However,
- /// unless creation and scheduling must be separated, StartNew is the recommended
- /// approach for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action action)
- {
- Task currTask = Task.InternalCurrent;
- return Task.InternalStartNew(currTask, action, null, m_defaultCancellationToken, GetDefaultScheduler(currTask),
- m_defaultCreationOptions, InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref name="action"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors
- /// and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution. However,
- /// unless creation and scheduling must be separated, StartNew is the recommended
- /// approach for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action action, CancellationToken cancellationToken)
- {
- Task currTask = Task.InternalCurrent;
- return Task.InternalStartNew(currTask, action, null, cancellationToken, GetDefaultScheduler(currTask),
- m_defaultCreationOptions, InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="action"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
- /// then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action action, TaskCreationOptions creationOptions)
- {
- Task currTask = Task.InternalCurrent;
- return Task.InternalStartNew(currTask, action, null, m_defaultCancellationToken, GetDefaultScheduler(currTask), creationOptions,
- InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
- /// <param name="scheduler">The <see
- /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created <see
- /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="action"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="scheduler"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
- /// then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
- {
- return Task.InternalStartNew(
- Task.InternalCurrentIfAttached(creationOptions), action, null, cancellationToken, scheduler, creationOptions,
- InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="action"/>
- /// delegate.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="action"/>
- /// argument is null.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
- /// then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action<object> action, object state)
- {
- Task currTask = Task.InternalCurrent;
- return Task.InternalStartNew(currTask, action, state, m_defaultCancellationToken, GetDefaultScheduler(currTask),
- m_defaultCreationOptions, InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="action"/>
- /// delegate.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="action"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
- /// then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action<object> action, object state, CancellationToken cancellationToken)
- {
- Task currTask = Task.InternalCurrent;
- return Task.InternalStartNew(currTask, action, state, cancellationToken, GetDefaultScheduler(currTask),
- m_defaultCreationOptions, InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="action"/>
- /// delegate.</param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="action"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
- /// then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action<object> action, object state, TaskCreationOptions creationOptions)
- {
- Task currTask = Task.InternalCurrent;
- return Task.InternalStartNew(currTask, action, state, m_defaultCancellationToken, GetDefaultScheduler(currTask),
- creationOptions, InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task">Task</see>.
- /// </summary>
- /// <param name="action">The action delegate to execute asynchronously.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="action"/>
- /// delegate.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task">Task.</see></param>
- /// <param name="scheduler">The <see
- /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created <see
- /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="action"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="scheduler"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a Task using one of its constructors and
- /// then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task StartNew(Action<object> action, object state, CancellationToken cancellationToken,
- TaskCreationOptions creationOptions, TaskScheduler scheduler)
- {
- return Task.InternalStartNew(
- Task.InternalCurrentIfAttached(creationOptions), action, state, cancellationToken, scheduler,
- creationOptions, InternalTaskOptions.None);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<TResult> function)
- {
- Task currTask = Task.InternalCurrent;
- return Task<TResult>.StartNew(currTask, function, m_defaultCancellationToken,
- m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<TResult> function, CancellationToken cancellationToken)
- {
- Task currTask = Task.InternalCurrent;
- return Task<TResult>.StartNew(currTask, function, cancellationToken,
- m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<TResult> function, TaskCreationOptions creationOptions)
- {
- Task currTask = Task.InternalCurrent;
- return Task<TResult>.StartNew(currTask, function, m_defaultCancellationToken,
- creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="scheduler">The <see
- /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created <see cref="T:System.Threading.Tasks.Task{TResult}">
- /// Task{TResult}</see>.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="scheduler"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
- {
- return Task<TResult>.StartNew(
- Task.InternalCurrentIfAttached(creationOptions), function, cancellationToken,
- creationOptions, InternalTaskOptions.None, scheduler);
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="function"/>
- /// delegate.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state)
- {
- Task currTask = Task.InternalCurrent;
- return Task<TResult>.StartNew(currTask, function, state, m_defaultCancellationToken,
- m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="function"/>
- /// delegate.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new <see cref="Task"/></param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state, CancellationToken cancellationToken)
- {
- Task currTask = Task.InternalCurrent;
- return Task<TResult>.StartNew(currTask, function, state, cancellationToken,
- m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="function"/>
- /// delegate.</param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
- {
- Task currTask = Task.InternalCurrent;
- return Task<TResult>.StartNew(currTask, function, state, m_defaultCancellationToken,
- creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask));
- }
- /// <summary>
- /// Creates and starts a <see cref="T:System.Threading.Tasks.Task{TResult}"/>.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="function">A function delegate that returns the future result to be available through
- /// the <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="function"/>
- /// delegate.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> that will be assigned to the new task.</param>
- /// <param name="creationOptions">A TaskCreationOptions value that controls the behavior of the
- /// created
- /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <param name="scheduler">The <see
- /// cref="T:System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created <see cref="T:System.Threading.Tasks.Task{TResult}">
- /// Task{TResult}</see>.</param>
- /// <returns>The started <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="function"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the <paramref
- /// name="scheduler"/>
- /// argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// Calling StartNew is functionally equivalent to creating a <see cref="Task{TResult}"/> using one
- /// of its constructors and then calling
- /// <see cref="System.Threading.Tasks.Task.Start()">Start</see> to schedule it for execution.
- /// However, unless creation and scheduling must be separated, StartNew is the recommended approach
- /// for both simplicity and performance.
- /// </remarks>
- public Task<TResult> StartNew<TResult>(Func<object, TResult> function, object state, CancellationToken cancellationToken,
- TaskCreationOptions creationOptions, TaskScheduler scheduler)
- {
- return Task<TResult>.StartNew(
- Task.InternalCurrentIfAttached(creationOptions), function, state, cancellationToken,
- creationOptions, InternalTaskOptions.None, scheduler);
- }
- //
- // FromAsync methods
- //
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that executes an end method action
- /// when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
- /// </summary>
- /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
- /// <paramref name="endMethod"/>.</param>
- /// <param name="endMethod">The action delegate that processes the completed <paramref
- /// name="asyncResult"/>.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="asyncResult"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the asynchronous
- /// operation.</returns>
- public Task FromAsync(
- IAsyncResult asyncResult,
- Action<IAsyncResult> endMethod)
- {
- return FromAsync(asyncResult, endMethod, m_defaultCreationOptions, DefaultScheduler);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that executes an end method action
- /// when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
- /// </summary>
- /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
- /// <paramref name="endMethod"/>.</param>
- /// <param name="endMethod">The action delegate that processes the completed <paramref
- /// name="asyncResult"/>.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="asyncResult"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the asynchronous
- /// operation.</returns>
- public Task FromAsync(
- IAsyncResult asyncResult,
- Action<IAsyncResult> endMethod,
- TaskCreationOptions creationOptions)
- {
- return FromAsync(asyncResult, endMethod, creationOptions, DefaultScheduler);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that executes an end method action
- /// when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
- /// </summary>
- /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
- /// <paramref name="endMethod"/>.</param>
- /// <param name="endMethod">The action delegate that processes the completed <paramref
- /// name="asyncResult"/>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the task that executes the end method.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="asyncResult"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the asynchronous
- /// operation.</returns>
- public Task FromAsync(
- IAsyncResult asyncResult,
- Action<IAsyncResult> endMethod,
- TaskCreationOptions creationOptions,
- TaskScheduler scheduler)
- {
- return TaskFactory<VoidTaskResult>.FromAsyncImpl(asyncResult, null, endMethod, creationOptions, scheduler);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync(
- Func<AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod,
- object state)
- {
- return FromAsync(beginMethod, endMethod, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync(
- Func<AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<VoidTaskResult>.FromAsyncImpl(beginMethod, null, endMethod, state, creationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/>
- /// delegate.</typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync<TArg1>(
- Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod,
- TArg1 arg1,
- object state)
- {
- return FromAsync(beginMethod, endMethod, arg1, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/>
- /// delegate.</typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync<TArg1>(
- Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod,
- TArg1 arg1, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<VoidTaskResult>.FromAsyncImpl(beginMethod, null, endMethod, arg1, state, creationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync<TArg1, TArg2>(
- Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod,
- TArg1 arg1, TArg2 arg2, object state)
- {
- return FromAsync(beginMethod, endMethod, arg1, arg2, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync<TArg1, TArg2>(
- Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod,
- TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<VoidTaskResult>.FromAsyncImpl(beginMethod, null, endMethod, arg1, arg2, state, creationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync<TArg1, TArg2, TArg3>(
- Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod,
- TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
- {
- return FromAsync(beginMethod, endMethod, arg1, arg2, arg3, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task">Task</see> that represents a pair of begin
- /// and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task">Task</see> that represents the
- /// asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task FromAsync<TArg1, TArg2, TArg3>(
- Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
- Action<IAsyncResult> endMethod,
- TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<VoidTaskResult>.FromAsyncImpl<TArg1, TArg2, TArg3>(beginMethod, null, endMethod, arg1, arg2, arg3, state, creationOptions);
- }
- //
- // Additional FromAsync() overloads used for inferencing convenience
- //
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
- /// method function when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
- /// <paramref name="endMethod"/>.</param>
- /// <param name="endMethod">The function delegate that processes the completed <paramref
- /// name="asyncResult"/>.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="asyncResult"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents the
- /// asynchronous operation.</returns>
- public Task<TResult> FromAsync<TResult>(
- IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
- {
- return TaskFactory<TResult>.FromAsyncImpl(asyncResult, endMethod, null, m_defaultCreationOptions, DefaultScheduler);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
- /// method function when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
- /// <paramref name="endMethod"/>.</param>
- /// <param name="endMethod">The function delegate that processes the completed <paramref
- /// name="asyncResult"/>.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="asyncResult"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents the
- /// asynchronous operation.</returns>
- public Task<TResult> FromAsync<TResult>(
- IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
- {
- return TaskFactory<TResult>.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, DefaultScheduler);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that executes an end
- /// method function when a specified <see cref="T:System.IAsyncResult">IAsyncResult</see> completes.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="asyncResult">The IAsyncResult whose completion should trigger the processing of the
- /// <paramref name="endMethod"/>.</param>
- /// <param name="endMethod">The function delegate that processes the completed <paramref
- /// name="asyncResult"/>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the task that executes the end method.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="asyncResult"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>A <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents the
- /// asynchronous operation.</returns>
- public Task<TResult> FromAsync<TResult>(
- IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
- {
- return TaskFactory<TResult>.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, scheduler);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TResult>(
- Func<AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, object state)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TResult>(
- Func<AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, state, creationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/> delegate.</typeparam>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TArg1, TResult>(
- Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, TArg1 arg1, object state)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/> delegate.</typeparam>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TArg1, TResult>(Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, TArg1 arg1, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, state, creationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/> delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TArg1, TArg2, TResult>(Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, object state)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/> delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TArg1, TArg2, TResult>(
- Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, state, creationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/> delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult>(
- Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, arg3, state, m_defaultCreationOptions);
- }
- /// <summary>
- /// Creates a <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that represents a pair of
- /// begin and end methods that conform to the Asynchronous Programming Model pattern.
- /// </summary>
- /// <typeparam name="TArg1">The type of the first argument passed to the <paramref
- /// name="beginMethod"/> delegate.</typeparam>
- /// <typeparam name="TArg2">The type of the second argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TArg3">The type of the third argument passed to <paramref name="beginMethod"/>
- /// delegate.</typeparam>
- /// <typeparam name="TResult">The type of the result available through the
- /// <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.
- /// </typeparam>
- /// <param name="beginMethod">The delegate that begins the asynchronous operation.</param>
- /// <param name="endMethod">The delegate that ends the asynchronous operation.</param>
- /// <param name="arg1">The first argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg2">The second argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="arg3">The third argument passed to the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <param name="creationOptions">The TaskCreationOptions value that controls the behavior of the
- /// created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="state">An object containing data to be used by the <paramref name="beginMethod"/>
- /// delegate.</param>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="beginMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="endMethod"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="creationOptions"/> argument specifies an invalid TaskCreationOptions
- /// value.</exception>
- /// <returns>The created <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see> that
- /// represents the asynchronous operation.</returns>
- /// <remarks>
- /// This method throws any exceptions thrown by the <paramref name="beginMethod"/>.
- /// </remarks>
- public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult>(
- Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
- Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
- {
- return TaskFactory<TResult>.FromAsyncImpl(beginMethod, endMethod, null, arg1, arg2, arg3, state, creationOptions);
- }
- /// <summary>
- /// Check validity of options passed to FromAsync method
- /// </summary>
- /// <param name="creationOptions">The options to be validated.</param>
- /// <param name="hasBeginMethod">determines type of FromAsync method that called this method</param>
- internal static void CheckFromAsyncOptions(TaskCreationOptions creationOptions, bool hasBeginMethod)
- {
- if (hasBeginMethod)
- {
- // Options detected here cause exceptions in FromAsync methods that take beginMethod as a parameter
- if ((creationOptions & TaskCreationOptions.LongRunning) != 0)
- throw new ArgumentOutOfRangeException(nameof(creationOptions), SR.Task_FromAsync_LongRunning);
- if ((creationOptions & TaskCreationOptions.PreferFairness) != 0)
- throw new ArgumentOutOfRangeException(nameof(creationOptions), SR.Task_FromAsync_PreferFairness);
- }
- // Check for general validity of options
- if ((creationOptions &
- ~(TaskCreationOptions.AttachedToParent |
- TaskCreationOptions.DenyChildAttach |
- TaskCreationOptions.HideScheduler |
- TaskCreationOptions.PreferFairness |
- TaskCreationOptions.LongRunning)) != 0)
- {
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions);
- }
- }
- //
- // ContinueWhenAll methods
- //
- // A Task<Task[]> that, given an initial collection of N tasks, will complete when
- // it has been invoked N times. This allows us to replace this logic:
- // Task<Task[]> promise = new Task<Task[]>(...);
- // int _count = tasksCopy.Length;
- // Action<Task> completionAction = delegate {if(Interlocked.Decrement(ref _count) == 0) promise.TrySetResult(tasksCopy);
- // for(int i=0; i<_count; i++)
- // tasksCopy[i].AddCompletionAction(completionAction);
- // with this logic:
- // CompletionOnCountdownPromise promise = new CompletionOnCountdownPromise(tasksCopy);
- // for(int i=0; i<tasksCopy.Length; i++) tasksCopy[i].AddCompletionAction(promise);
- // which saves a few allocations.
- //
- // Used in TaskFactory.CommonCWAllLogic(Task[]), below.
- private sealed class CompleteOnCountdownPromise : Task<Task[]>, ITaskCompletionAction
- {
- private readonly Task[] _tasks;
- private int _count;
- internal CompleteOnCountdownPromise(Task[] tasksCopy) : base()
- {
- Debug.Assert((tasksCopy != null) && (tasksCopy.Length > 0), "Expected non-null task array with at least one element in it");
- _tasks = tasksCopy;
- _count = tasksCopy.Length;
- if (DebuggerSupport.LoggingOn)
- DebuggerSupport.TraceOperationCreation(CausalityTraceLevel.Required, this, "TaskFactory.ContinueWhenAll", 0);
- DebuggerSupport.AddToActiveTasks(this);
- }
- public void Invoke(Task completingTask)
- {
- if (DebuggerSupport.LoggingOn)
- DebuggerSupport.TraceOperationRelation(CausalityTraceLevel.Important, this, CausalityRelation.Join);
- if (completingTask.IsWaitNotificationEnabled) this.SetNotificationForWaitCompletion(enabled: true);
- if (Interlocked.Decrement(ref _count) == 0)
- {
- if (DebuggerSupport.LoggingOn)
- DebuggerSupport.TraceOperationCompletion(CausalityTraceLevel.Required, this, AsyncStatus.Completed);
- DebuggerSupport.RemoveFromActiveTasks(this);
- TrySetResult(_tasks);
- }
- Debug.Assert(_count >= 0, "Count should never go below 0");
- }
- public bool InvokeMayRunArbitraryCode { get { return true; } }
- /// <summary>
- /// Returns whether we should notify the debugger of a wait completion. This returns
- /// true iff at least one constituent task has its bit set.
- /// </summary>
- internal override bool ShouldNotifyDebuggerOfWaitCompletion
- {
- get
- {
- return
- base.ShouldNotifyDebuggerOfWaitCompletion &&
- Task.AnyTaskRequiresNotifyDebuggerOfWaitCompletion(_tasks);
- }
- }
- }
- // Performs some logic common to all ContinueWhenAll() overloads
- internal static Task<Task[]> CommonCWAllLogic(Task[] tasksCopy)
- {
- Debug.Assert(tasksCopy != null);
- // Create a promise task to be returned to the user
- CompleteOnCountdownPromise promise = new CompleteOnCountdownPromise(tasksCopy);
- for (int i = 0; i < tasksCopy.Length; i++)
- {
- if (tasksCopy[i].IsCompleted) promise.Invoke(tasksCopy[i]); // Short-circuit the completion action, if possible
- else tasksCopy[i].AddCompletionAction(promise); // simple completion action
- }
- return promise;
- }
- // A Task<Task<T>[]> that, given an initial collection of N tasks, will complete when
- // it has been invoked N times. See comments for non-generic CompleteOnCountdownPromise class.
- //
- // Used in TaskFactory.CommonCWAllLogic<TResult>(Task<TResult>[]), below.
- private sealed class CompleteOnCountdownPromise<T> : Task<Task<T>[]>, ITaskCompletionAction
- {
- private readonly Task<T>[] _tasks;
- private int _count;
- internal CompleteOnCountdownPromise(Task<T>[] tasksCopy) : base()
- {
- Debug.Assert((tasksCopy != null) && (tasksCopy.Length > 0), "Expected non-null task array with at least one element in it");
- _tasks = tasksCopy;
- _count = tasksCopy.Length;
- if (DebuggerSupport.LoggingOn)
- DebuggerSupport.TraceOperationCreation(CausalityTraceLevel.Required, this, "TaskFactory.ContinueWhenAll<>", 0);
- DebuggerSupport.AddToActiveTasks(this);
- }
- public void Invoke(Task completingTask)
- {
- if (DebuggerSupport.LoggingOn)
- DebuggerSupport.TraceOperationRelation(CausalityTraceLevel.Important, this, CausalityRelation.Join);
- if (completingTask.IsWaitNotificationEnabled) this.SetNotificationForWaitCompletion(enabled: true);
- if (Interlocked.Decrement(ref _count) == 0)
- {
- if (DebuggerSupport.LoggingOn)
- DebuggerSupport.TraceOperationCompletion(CausalityTraceLevel.Required, this, AsyncStatus.Completed);
- DebuggerSupport.RemoveFromActiveTasks(this);
- TrySetResult(_tasks);
- }
- Debug.Assert(_count >= 0, "Count should never go below 0");
- }
- public bool InvokeMayRunArbitraryCode { get { return true; } }
- /// <summary>
- /// Returns whether we should notify the debugger of a wait completion. This returns
- /// true iff at least one constituent task has its bit set.
- /// </summary>
- internal override bool ShouldNotifyDebuggerOfWaitCompletion
- {
- get
- {
- return
- base.ShouldNotifyDebuggerOfWaitCompletion &&
- Task.AnyTaskRequiresNotifyDebuggerOfWaitCompletion(_tasks);
- }
- }
- }
- internal static Task<Task<T>[]> CommonCWAllLogic<T>(Task<T>[] tasksCopy)
- {
- Debug.Assert(tasksCopy != null);
- // Create a promise task to be returned to the user
- CompleteOnCountdownPromise<T> promise = new CompleteOnCountdownPromise<T>(tasksCopy);
- for (int i = 0; i < tasksCopy.Length; i++)
- {
- if (tasksCopy[i].IsCompleted) promise.Invoke(tasksCopy[i]); // Short-circuit the completion action, if possible
- else tasksCopy[i].AddCompletionAction(promise); // simple completion action
- }
- return promise;
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in
- /// the <paramref name="tasks"/> array have completed.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in
- /// the <paramref name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
- /// name="tasks"/> array have completed.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction, TaskContinuationOptions continuationOptions)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
- /// name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
- TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in
- /// the <paramref name="tasks"/> array have completed.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in
- /// the <paramref name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
- CancellationToken cancellationToken)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
- /// name="tasks"/> array have completed.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
- TaskContinuationOptions continuationOptions)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationAction">The action delegate to execute when all tasks in the <paramref
- /// name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task ContinueWhenAll<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
- CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction, CancellationToken cancellationToken)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction, TaskContinuationOptions continuationOptions)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task<TResult> ContinueWhenAll<TResult>(Task[] tasks, Func<Task[], TResult> continuationFunction, CancellationToken cancellationToken,
- TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction,
- CancellationToken cancellationToken)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction,
- TaskContinuationOptions continuationOptions)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of a set of provided Tasks.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue.</param>
- /// <param name="continuationFunction">The function delegate to execute when all tasks in the
- /// <paramref name="tasks"/> array have completed.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAll.
- /// </remarks>
- public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>[], TResult> continuationFunction,
- CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAllImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
- }
- //
- // ContinueWhenAny methods
- //
- // A Task<Task> that will be completed the first time that Invoke is called.
- // It allows us to replace this logic:
- // Task<Task> promise = new Task<Task>(...);
- // Action<Task> completionAction = delegate(Task completingTask) { promise.TrySetResult(completingTask); }
- // for(int i=0; i<tasksCopy.Length; i++) tasksCopy[i].AddCompletionAction(completionAction);
- // with this logic:
- // CompletionOnInvokePromise promise = new CompletionOnInvokePromise(tasksCopy);
- // for(int i=0; i<tasksCopy.Length; i++) tasksCopy[i].AddCompletionAction(promise);
- // which saves a couple of allocations.
- //
- // Used in TaskFactory.CommonCWAnyLogic(), below.
- internal sealed class CompleteOnInvokePromise : Task<Task>, ITaskCompletionAction
- {
- private IList<Task> _tasks; // must track this for cleanup
- private int m_firstTaskAlreadyCompleted;
- public CompleteOnInvokePromise(IList<Task> tasks) : base()
- {
- Debug.Assert(tasks != null, "Expected non-null collection of tasks");
- _tasks = tasks;
- if (DebuggerSupport.LoggingOn)
- DebuggerSupport.TraceOperationCreation(CausalityTraceLevel.Required, this, "TaskFactory.ContinueWhenAny", 0);
- DebuggerSupport.AddToActiveTasks(this);
- }
- public void Invoke(Task completingTask)
- {
- if (m_firstTaskAlreadyCompleted == 0 &&
- Interlocked.Exchange(ref m_firstTaskAlreadyCompleted, 1) == 0)
- {
- // This was the first Task to complete.
- bool success = TrySetResult(completingTask);
- Debug.Assert(success, "Only one task should have gotten to this point, and thus this must be successful.");
- // We need to remove continuations that may be left straggling on other tasks.
- // Otherwise, repeated calls to WhenAny using the same task could leak actions.
- // This may also help to avoided unnecessary invocations of this whenComplete delegate.
- // Note that we may be attempting to remove a continuation from a task that hasn't had it
- // added yet; while there's overhead there, the operation won't hurt anything.
- if (DebuggerSupport.LoggingOn)
- {
- DebuggerSupport.TraceOperationRelation(CausalityTraceLevel.Important, this, CausalityRelation.Choice);
- DebuggerSupport.TraceOperationCompletion(CausalityTraceLevel.Required, this, AsyncStatus.Completed);
- }
- DebuggerSupport.RemoveFromActiveTasks(this);
- var tasks = _tasks;
- int numTasks = tasks.Count;
- for (int i = 0; i < numTasks; i++)
- {
- var task = tasks[i];
- if (task != null && // if an element was erroneously nulled out concurrently, just skip it; worst case is we don't remove a continuation
- !task.IsCompleted)
- task.RemoveContinuation(this);
- }
- _tasks = null;
- }
- }
- public bool InvokeMayRunArbitraryCode { get { return true; } }
- }
- // Common ContinueWhenAny logic
- // If the tasks list is not an array, it must be an internal defensive copy so that
- // we don't need to be concerned about concurrent modifications to the list. If the task list
- // is an array, it should be a defensive copy if this functionality is being used
- // asynchronously (e.g. WhenAny) rather than synchronously (e.g. WaitAny).
- internal static Task<Task> CommonCWAnyLogic(IList<Task> tasks)
- {
- Debug.Assert(tasks != null);
- // Create a promise task to be returned to the user.
- // (If this logic ever changes, also update CommonCWAnyLogicCleanup.)
- var promise = new CompleteOnInvokePromise(tasks);
- // At the completion of any of the tasks, complete the promise.
- bool checkArgsOnly = false;
- int numTasks = tasks.Count;
- for (int i = 0; i < numTasks; i++)
- {
- var task = tasks[i];
- if (task == null) throw new ArgumentException(SR.Task_MultiTaskContinuation_NullTask, nameof(tasks));
- if (checkArgsOnly) continue;
- // If the promise has already completed, don't bother with checking any more tasks.
- if (promise.IsCompleted)
- {
- checkArgsOnly = true;
- }
- // If a task has already completed, complete the promise.
- else if (task.IsCompleted)
- {
- promise.Invoke(task);
- checkArgsOnly = true;
- }
- // Otherwise, add the completion action and keep going.
- else
- {
- task.AddCompletionAction(promise);
- if (promise.IsCompleted)
- {
- // One of the previous tasks that already had its continuation registered may have
- // raced to complete with our adding the continuation to this task. The completion
- // routine would have gone through and removed the continuation from all of the tasks
- // with which it was already registered, but if the race causes this continuation to
- // be added after that, it'll never be removed. As such, after adding the continuation,
- // we check to see whether the promise has already completed, and if it has, we try to
- // manually remove the continuation from this task. If it was already removed, it'll be
- // a nop, and if we race to remove it, the synchronization in RemoveContinuation will
- // keep things consistent.
- task.RemoveContinuation(promise);
- }
- }
- }
- return promise;
- }
- /// <summary>
- /// Cleans up the operations performed by CommonCWAnyLogic in a case where
- /// the created continuation task is being discarded.
- /// </summary>
- /// <param name="continuation">The task returned from CommonCWAnyLogic.</param>
- internal static void CommonCWAnyLogicCleanup(Task<Task> continuation)
- {
- // Force cleanup of the promise (e.g. removing continuations from each
- // constituent task), by completing the promise with any value.
- ((CompleteOnInvokePromise)continuation).Invoke(null);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
- /// name="tasks"/> array completes.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
- /// name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
- /// name="tasks"/> array completes.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the <paramref
- /// name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task ContinueWhenAny(Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken,
- TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction, CancellationToken cancellationToken)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction, TaskContinuationOptions continuationOptions)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task<TResult> ContinueWhenAny<TResult>(Task[] tasks, Func<Task, TResult> continuationFunction, CancellationToken cancellationToken,
- TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction,
- CancellationToken cancellationToken)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction,
- TaskContinuationOptions continuationOptions)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TResult">The type of the result that is returned by the <paramref
- /// name="continuationFunction"/>
- /// delegate and associated with the created <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</typeparam>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationFunction">The function delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task{TResult}">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationFunction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult>(Task<TAntecedentResult>[] tasks, Func<Task<TAntecedentResult>, TResult> continuationFunction,
- CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction));
- return TaskFactory<TResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
- CancellationToken cancellationToken)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
- TaskContinuationOptions continuationOptions)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler);
- }
- /// <summary>
- /// Creates a continuation <see cref="T:System.Threading.Tasks.Task">Task</see>
- /// that will be started upon the completion of any Task in the provided set.
- /// </summary>
- /// <typeparam name="TAntecedentResult">The type of the result of the antecedent <paramref name="tasks"/>.</typeparam>
- /// <param name="tasks">The array of tasks from which to continue when one task completes.</param>
- /// <param name="continuationAction">The action delegate to execute when one task in the
- /// <paramref name="tasks"/> array completes.</param>
- /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// that will be assigned to the new continuation task.</param>
- /// <param name="continuationOptions">The <see cref="System.Threading.Tasks.TaskContinuationOptions">
- /// TaskContinuationOptions</see> value that controls the behavior of
- /// the created continuation <see cref="T:System.Threading.Tasks.Task">Task</see>.</param>
- /// <param name="scheduler">The <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see>
- /// that is used to schedule the created continuation <see
- /// cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
- /// <returns>The new continuation <see cref="T:System.Threading.Tasks.Task"/>.</returns>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="continuationAction"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentNullException">The exception that is thrown when the
- /// <paramref name="scheduler"/> argument is null.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array contains a null value.</exception>
- /// <exception cref="T:System.ArgumentException">The exception that is thrown when the
- /// <paramref name="tasks"/> array is empty.</exception>
- /// <exception cref="T:System.ArgumentOutOfRangeException">The exception that is thrown when the
- /// <paramref name="continuationOptions"/> argument specifies an invalid TaskContinuationOptions
- /// value.</exception>
- /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see>
- /// has already been disposed.
- /// </exception>
- /// <remarks>
- /// The NotOn* and OnlyOn* <see cref="System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>,
- /// which constrain for which <see cref="System.Threading.Tasks.TaskStatus">TaskStatus</see> states a continuation
- /// will be executed, are illegal with ContinueWhenAny.
- /// </remarks>
- public Task ContinueWhenAny<TAntecedentResult>(Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
- CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
- {
- if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction));
- return TaskFactory<VoidTaskResult>.ContinueWhenAnyImpl<TAntecedentResult>(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler);
- }
- // Check task array and return a defensive copy.
- // Used with ContinueWhenAll()/ContinueWhenAny().
- internal static Task[] CheckMultiContinuationTasksAndCopy(Task[] tasks)
- {
- if (tasks == null)
- throw new ArgumentNullException(nameof(tasks));
- if (tasks.Length == 0)
- throw new ArgumentException(SR.Task_MultiTaskContinuation_EmptyTaskList, nameof(tasks));
- Task[] tasksCopy = new Task[tasks.Length];
- for (int i = 0; i < tasks.Length; i++)
- {
- tasksCopy[i] = tasks[i];
- if (tasksCopy[i] == null)
- throw new ArgumentException(SR.Task_MultiTaskContinuation_NullTask, nameof(tasks));
- }
- return tasksCopy;
- }
- internal static Task<TResult>[] CheckMultiContinuationTasksAndCopy<TResult>(Task<TResult>[] tasks)
- {
- if (tasks == null)
- throw new ArgumentNullException(nameof(tasks));
- if (tasks.Length == 0)
- throw new ArgumentException(SR.Task_MultiTaskContinuation_EmptyTaskList, nameof(tasks));
- Task<TResult>[] tasksCopy = new Task<TResult>[tasks.Length];
- for (int i = 0; i < tasks.Length; i++)
- {
- tasksCopy[i] = tasks[i];
- if (tasksCopy[i] == null)
- throw new ArgumentException(SR.Task_MultiTaskContinuation_NullTask, nameof(tasks));
- }
- return tasksCopy;
- }
- // Throw an exception if "options" argument specifies illegal options
- internal static void CheckMultiTaskContinuationOptions(TaskContinuationOptions continuationOptions)
- {
- // Construct a mask to check for illegal options
- const TaskContinuationOptions NotOnAny = TaskContinuationOptions.NotOnCanceled |
- TaskContinuationOptions.NotOnFaulted |
- TaskContinuationOptions.NotOnRanToCompletion;
- // Check that LongRunning and ExecuteSynchronously are not specified together
- const TaskContinuationOptions illegalMask = TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.LongRunning;
- if ((continuationOptions & illegalMask) == illegalMask)
- {
- throw new ArgumentOutOfRangeException(nameof(continuationOptions), SR.Task_ContinueWith_ESandLR);
- }
- // Check that no nonsensical options are specified.
- if ((continuationOptions & ~(
- TaskContinuationOptions.LongRunning |
- TaskContinuationOptions.PreferFairness |
- TaskContinuationOptions.AttachedToParent |
- TaskContinuationOptions.DenyChildAttach |
- TaskContinuationOptions.HideScheduler |
- TaskContinuationOptions.LazyCancellation |
- NotOnAny |
- TaskContinuationOptions.ExecuteSynchronously)) != 0)
- {
- throw new ArgumentOutOfRangeException(nameof(continuationOptions));
- }
- // Check that no "fire" options are specified.
- if ((continuationOptions & NotOnAny) != 0)
- throw new ArgumentOutOfRangeException(nameof(continuationOptions), SR.Task_MultiTaskContinuation_FireOptions);
- }
- }
- }
|