TaskFactory.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. //
  2. // TaskFactory.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if NET_4_0 || MOBILE
  27. using System;
  28. using System.Threading;
  29. namespace System.Threading.Tasks
  30. {
  31. public class TaskFactory
  32. {
  33. TaskScheduler scheduler;
  34. TaskCreationOptions creationOptions;
  35. TaskContinuationOptions continuationOptions;
  36. CancellationToken cancellationToken;
  37. #region ctors
  38. public TaskFactory ()
  39. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  40. {
  41. }
  42. public TaskFactory (CancellationToken cancellationToken)
  43. : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  44. {
  45. }
  46. public TaskFactory (TaskScheduler scheduler)
  47. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  48. {
  49. }
  50. public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  51. : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
  52. {
  53. }
  54. public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
  55. TaskScheduler scheduler)
  56. {
  57. this.cancellationToken = cancellationToken;
  58. this.scheduler = scheduler;
  59. this.creationOptions = creationOptions;
  60. this.continuationOptions = continuationOptions;
  61. }
  62. #endregion
  63. #region StartNew for Task
  64. public Task StartNew (Action action)
  65. {
  66. return StartNew (action, cancellationToken, creationOptions, scheduler);
  67. }
  68. public Task StartNew (Action action, CancellationToken cancellationToken)
  69. {
  70. return StartNew (action, cancellationToken, creationOptions, scheduler);
  71. }
  72. public Task StartNew (Action action, TaskCreationOptions creationOptions)
  73. {
  74. return StartNew (action, cancellationToken, creationOptions, scheduler);
  75. }
  76. public Task StartNew (Action<object> action, object state)
  77. {
  78. return StartNew (action, state, cancellationToken, creationOptions, scheduler);
  79. }
  80. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
  81. {
  82. return StartNew (action, state, cancellationToken, creationOptions, scheduler);
  83. }
  84. public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
  85. {
  86. return StartNew (action, state, cancellationToken, creationOptions, scheduler);
  87. }
  88. public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  89. {
  90. Task t = new Task (action, cancellationToken, creationOptions);
  91. t.Start (scheduler);
  92. return t;
  93. }
  94. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
  95. TaskScheduler scheduler)
  96. {
  97. Task t = new Task (action, state, cancellationToken, creationOptions);
  98. t.Start (scheduler);
  99. return t;
  100. }
  101. #endregion
  102. #region StartNew for Task<TResult>
  103. public Task<TResult> StartNew<TResult> (Func<TResult> function)
  104. {
  105. return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
  106. }
  107. public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
  108. {
  109. return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
  110. }
  111. public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
  112. {
  113. return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
  114. }
  115. public Task<TResult> StartNew<TResult> (Func<TResult> function,
  116. CancellationToken cancellationToken,
  117. TaskCreationOptions creationOptions,
  118. TaskScheduler scheduler)
  119. {
  120. return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  121. }
  122. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
  123. {
  124. return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  125. }
  126. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  127. {
  128. return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  129. }
  130. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  131. {
  132. return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  133. }
  134. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
  135. CancellationToken cancellationToken,
  136. TaskCreationOptions creationOptions,
  137. TaskScheduler scheduler)
  138. {
  139. Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
  140. t.Start (scheduler);
  141. return t;
  142. }
  143. #endregion
  144. #region Continue
  145. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
  146. {
  147. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  148. }
  149. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
  150. {
  151. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  152. }
  153. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  154. {
  155. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  156. }
  157. public Task ContinueWhenAny (Task[] tasks,
  158. Action<Task> continuationAction,
  159. CancellationToken cancellationToken,
  160. TaskContinuationOptions continuationOptions,
  161. TaskScheduler scheduler)
  162. {
  163. var ourTasks = (Task[])tasks.Clone ();
  164. AtomicBoolean trigger = new AtomicBoolean ();
  165. Task commonContinuation = new Task (null);
  166. foreach (Task t in ourTasks) {
  167. Task cont = new Task ((o) => continuationAction ((Task)o), t, cancellationToken, creationOptions, t);
  168. t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
  169. cont.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
  170. }
  171. return commonContinuation;
  172. }
  173. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  174. Action<Task<TAntecedentResult>> continuationAction)
  175. {
  176. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  177. }
  178. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  179. Action<Task<TAntecedentResult>> continuationAction,
  180. CancellationToken cancellationToken)
  181. {
  182. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  183. }
  184. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  185. Action<Task<TAntecedentResult>> continuationAction,
  186. TaskContinuationOptions continuationOptions)
  187. {
  188. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  189. }
  190. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  191. Action<Task<TAntecedentResult>> continuationAction,
  192. CancellationToken cancellationToken,
  193. TaskContinuationOptions continuationOptions,
  194. TaskScheduler scheduler)
  195. {
  196. return ContinueWhenAny ((Task[]) tasks,
  197. (o) => continuationAction ((Task<TAntecedentResult>)o),
  198. cancellationToken, continuationOptions, scheduler);
  199. }
  200. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
  201. {
  202. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  203. }
  204. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  205. Func<Task, TResult> continuationFunction,
  206. CancellationToken cancellationToken)
  207. {
  208. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  209. }
  210. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  211. Func<Task, TResult> continuationFunction,
  212. TaskContinuationOptions continuationOptions)
  213. {
  214. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  215. }
  216. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  217. Func<Task, TResult> continuationFunction,
  218. CancellationToken cancellationToken,
  219. TaskContinuationOptions continuationOptions,
  220. TaskScheduler scheduler)
  221. {
  222. var ourTasks = (Task[])tasks.Clone ();
  223. AtomicBoolean trigger = new AtomicBoolean ();
  224. TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
  225. foreach (Task t in ourTasks) {
  226. Task cont = new Task ((o) => source.SetResult (continuationFunction ((Task)o)), t, cancellationToken, creationOptions, t);
  227. t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
  228. }
  229. return source.Task;
  230. }
  231. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  232. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  233. {
  234. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  235. }
  236. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  237. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  238. CancellationToken cancellationToken)
  239. {
  240. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  241. }
  242. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  243. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  244. TaskContinuationOptions continuationOptions)
  245. {
  246. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  247. }
  248. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  249. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  250. CancellationToken cancellationToken,
  251. TaskContinuationOptions continuationOptions,
  252. TaskScheduler scheduler)
  253. {
  254. return ContinueWhenAny<TResult> ((Task[])tasks,
  255. (t) => continuationFunction((Task<TAntecedentResult>)t),
  256. cancellationToken,
  257. continuationOptions,
  258. scheduler);
  259. }
  260. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
  261. {
  262. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  263. }
  264. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
  265. {
  266. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  267. }
  268. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
  269. TaskContinuationOptions continuationOptions)
  270. {
  271. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  272. }
  273. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
  274. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  275. {
  276. var ourTasks = (Task[])tasks.Clone ();
  277. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  278. Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  279. foreach (Task t in ourTasks)
  280. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  281. return cont;
  282. }
  283. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  284. Action<Task<TAntecedentResult>[]> continuationAction)
  285. {
  286. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  287. }
  288. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  289. Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
  290. {
  291. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  292. }
  293. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  294. TaskContinuationOptions continuationOptions)
  295. {
  296. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  297. }
  298. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  299. Action<Task<TAntecedentResult>[]> continuationAction,
  300. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
  301. TaskScheduler scheduler)
  302. {
  303. return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
  304. continuationOptions, scheduler);
  305. }
  306. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
  307. {
  308. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  309. }
  310. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  311. TaskContinuationOptions continuationOptions)
  312. {
  313. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  314. }
  315. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  316. CancellationToken cancellationToken)
  317. {
  318. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  319. }
  320. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  321. CancellationToken cancellationToken,
  322. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  323. {
  324. var ourTasks = (Task[])tasks.Clone ();
  325. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  326. Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  327. foreach (Task t in ourTasks)
  328. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  329. return cont;
  330. }
  331. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  332. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  333. {
  334. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  335. }
  336. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  337. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  338. TaskContinuationOptions continuationOptions)
  339. {
  340. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  341. }
  342. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  343. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  344. CancellationToken cancellationToken)
  345. {
  346. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  347. }
  348. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  349. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  350. CancellationToken cancellationToken,
  351. TaskContinuationOptions continuationOptions,
  352. TaskScheduler scheduler)
  353. {
  354. return ContinueWhenAll<TResult> ((Task[]) tasks,
  355. (o) => continuationFunction (tasks),
  356. cancellationToken,
  357. continuationOptions, scheduler);
  358. }
  359. #endregion
  360. #region FromAsync
  361. // For these methods to work we first have to convert the ThreadPool to use Tasks as it
  362. // is doing in 4.0, then all that is remaining is to identify the Task on which is
  363. // run the async operation (probably with some additional state in a IAsyncResult subclass)
  364. // and call its ContinueWith method accordingly
  365. const string errorMsg = "Mono's thread pool doesn't support this operation yet";
  366. [MonoLimitation(errorMsg)]
  367. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
  368. {
  369. return FromAsync (asyncResult, endMethod, creationOptions);
  370. }
  371. [MonoLimitation(errorMsg)]
  372. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
  373. TaskCreationOptions creationOptions)
  374. {
  375. return FromAsync (asyncResult, endMethod, creationOptions);
  376. }
  377. [MonoLimitation(errorMsg)]
  378. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
  379. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  380. {
  381. throw new NotSupportedException (errorMsg);
  382. }
  383. [MonoLimitation(errorMsg)]
  384. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  385. {
  386. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  387. }
  388. [MonoLimitation(errorMsg)]
  389. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  390. TaskCreationOptions creationOptions)
  391. {
  392. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  393. }
  394. [MonoLimitation(errorMsg)]
  395. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  396. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  397. {
  398. throw new NotSupportedException (errorMsg);
  399. }
  400. [MonoLimitation(errorMsg)]
  401. public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  402. object state)
  403. {
  404. return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
  405. }
  406. [MonoLimitation(errorMsg)]
  407. public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  408. object state, TaskCreationOptions creationOptions)
  409. {
  410. return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
  411. }
  412. [MonoLimitation(errorMsg)]
  413. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  414. TArg1 arg1, object state)
  415. {
  416. throw new NotSupportedException (errorMsg);
  417. }
  418. [MonoLimitation(errorMsg)]
  419. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  420. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  421. {
  422. throw new NotSupportedException (errorMsg);
  423. }
  424. [MonoLimitation(errorMsg)]
  425. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  426. TArg1 arg1, TArg2 arg2, object state)
  427. {
  428. throw new NotSupportedException (errorMsg);
  429. }
  430. [MonoLimitation(errorMsg)]
  431. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  432. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  433. {
  434. throw new NotSupportedException (errorMsg);
  435. }
  436. [MonoLimitation(errorMsg)]
  437. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  438. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  439. {
  440. throw new NotSupportedException (errorMsg);
  441. }
  442. [MonoLimitation(errorMsg)]
  443. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  444. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  445. {
  446. throw new NotSupportedException (errorMsg);
  447. }
  448. [MonoLimitation(errorMsg)]
  449. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  450. Func<IAsyncResult, TResult> endMethod,
  451. object state)
  452. {
  453. throw new NotSupportedException (errorMsg);
  454. }
  455. [MonoLimitation(errorMsg)]
  456. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  457. Func<IAsyncResult, TResult> endMethod,
  458. object state, TaskCreationOptions creationOptions)
  459. {
  460. throw new NotSupportedException (errorMsg);
  461. }
  462. [MonoLimitation(errorMsg)]
  463. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  464. Func<IAsyncResult, TResult> endMethod,
  465. TArg1 arg1, object state)
  466. {
  467. throw new NotSupportedException (errorMsg);
  468. }
  469. [MonoLimitation(errorMsg)]
  470. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  471. Func<IAsyncResult, TResult> endMethod,
  472. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  473. {
  474. throw new NotSupportedException (errorMsg);
  475. }
  476. [MonoLimitation(errorMsg)]
  477. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  478. Func<IAsyncResult, TResult> endMethod,
  479. TArg1 arg1, TArg2 arg2, object state)
  480. {
  481. throw new NotSupportedException (errorMsg);
  482. }
  483. [MonoLimitation(errorMsg)]
  484. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  485. Func<IAsyncResult, TResult> endMethod,
  486. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  487. {
  488. throw new NotSupportedException (errorMsg);
  489. }
  490. [MonoLimitation(errorMsg)]
  491. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  492. Func<IAsyncResult, TResult> endMethod,
  493. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  494. {
  495. throw new NotSupportedException (errorMsg);
  496. }
  497. [MonoLimitation(errorMsg)]
  498. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  499. Func<IAsyncResult, TResult> endMethod,
  500. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  501. TaskCreationOptions creationOptions)
  502. {
  503. throw new NotSupportedException (errorMsg);
  504. }
  505. #endregion
  506. public TaskScheduler Scheduler {
  507. get {
  508. return scheduler;
  509. }
  510. }
  511. public TaskContinuationOptions ContinuationOptions {
  512. get {
  513. return continuationOptions;
  514. }
  515. }
  516. public TaskCreationOptions CreationOptions {
  517. get {
  518. return creationOptions;
  519. }
  520. }
  521. public CancellationToken CancellationToken {
  522. get {
  523. return cancellationToken;
  524. }
  525. }
  526. }
  527. public class TaskFactory<TResult>
  528. {
  529. TaskScheduler scheduler;
  530. TaskCreationOptions creationOptions;
  531. TaskContinuationOptions continuationOptions;
  532. CancellationToken cancellationToken;
  533. TaskFactory parent;
  534. #region ctors
  535. public TaskFactory ()
  536. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  537. {
  538. }
  539. public TaskFactory (TaskScheduler scheduler)
  540. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  541. {
  542. }
  543. public TaskFactory (CancellationToken cancellationToken)
  544. : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  545. {
  546. }
  547. public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  548. : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
  549. {
  550. }
  551. public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
  552. TaskScheduler scheduler)
  553. {
  554. this.cancellationToken = cancellationToken;
  555. this.scheduler = scheduler;
  556. this.creationOptions = creationOptions;
  557. this.continuationOptions = continuationOptions;
  558. this.parent = new TaskFactory (cancellationToken, creationOptions, continuationOptions, scheduler);
  559. }
  560. #endregion
  561. #region StartNew for Task<TResult>
  562. public Task<TResult> StartNew (Func<TResult> function)
  563. {
  564. return StartNew (function, cancellationToken, creationOptions, scheduler);
  565. }
  566. public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
  567. {
  568. return StartNew (function, cancellationToken, creationOptions, scheduler);
  569. }
  570. public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
  571. {
  572. return StartNew (function, cancellationToken, creationOptions, scheduler);
  573. }
  574. public Task<TResult> StartNew (Func<TResult> function,
  575. CancellationToken cancellationToken,
  576. TaskCreationOptions creationOptions,
  577. TaskScheduler scheduler)
  578. {
  579. return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  580. }
  581. public Task<TResult> StartNew (Func<object, TResult> function, object state)
  582. {
  583. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  584. }
  585. public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  586. {
  587. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  588. }
  589. public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  590. {
  591. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  592. }
  593. public Task<TResult> StartNew (Func<object, TResult> function, object state,
  594. CancellationToken cancellationToken,
  595. TaskCreationOptions creationOptions,
  596. TaskScheduler scheduler)
  597. {
  598. return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  599. }
  600. #endregion
  601. #region Continue
  602. public Task<TResult> ContinueWhenAny (Task[] tasks,
  603. Func<Task, TResult> continuationFunction)
  604. {
  605. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  606. }
  607. public Task<TResult> ContinueWhenAny (Task[] tasks,
  608. Func<Task, TResult> continuationFunction,
  609. CancellationToken cancellationToken)
  610. {
  611. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  612. }
  613. public Task<TResult> ContinueWhenAny (Task[] tasks,
  614. Func<Task, TResult> continuationFunction,
  615. TaskContinuationOptions continuationOptions)
  616. {
  617. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  618. }
  619. public Task<TResult> ContinueWhenAny (Task[] tasks,
  620. Func<Task, TResult> continuationFunction,
  621. CancellationToken cancellationToken,
  622. TaskContinuationOptions continuationOptions,
  623. TaskScheduler scheduler)
  624. {
  625. return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  626. }
  627. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  628. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  629. {
  630. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  631. }
  632. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  633. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  634. CancellationToken cancellationToken)
  635. {
  636. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  637. }
  638. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  639. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  640. TaskContinuationOptions continuationOptions)
  641. {
  642. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  643. }
  644. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  645. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  646. CancellationToken cancellationToken,
  647. TaskContinuationOptions continuationOptions,
  648. TaskScheduler scheduler)
  649. {
  650. return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  651. }
  652. public Task<TResult> ContinueWhenAll (Task[] tasks,
  653. Func<Task[], TResult> continuationFunction)
  654. {
  655. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  656. }
  657. public Task<TResult> ContinueWhenAll (Task[] tasks,
  658. Func<Task[], TResult> continuationFunction,
  659. TaskContinuationOptions continuationOptions)
  660. {
  661. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  662. }
  663. public Task<TResult> ContinueWhenAll (Task[] tasks,
  664. Func<Task[], TResult> continuationFunction,
  665. CancellationToken cancellationToken)
  666. {
  667. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  668. }
  669. public Task<TResult> ContinueWhenAll (Task[] tasks,
  670. Func<Task[], TResult> continuationFunction,
  671. CancellationToken cancellationToken,
  672. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  673. {
  674. return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  675. }
  676. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  677. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  678. {
  679. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  680. }
  681. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  682. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  683. TaskContinuationOptions continuationOptions)
  684. {
  685. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  686. }
  687. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  688. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  689. CancellationToken cancellationToken)
  690. {
  691. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  692. }
  693. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  694. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  695. CancellationToken cancellationToken,
  696. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  697. {
  698. return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  699. }
  700. #endregion
  701. #region FromAsync
  702. const string errorMsg = "Mono's thread pool doesn't support this operation yet";
  703. [MonoLimitation(errorMsg)]
  704. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  705. {
  706. return FromAsync (asyncResult, endMethod, creationOptions);
  707. }
  708. [MonoLimitation(errorMsg)]
  709. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  710. TaskCreationOptions creationOptions)
  711. {
  712. return FromAsync (asyncResult, endMethod, creationOptions);
  713. }
  714. [MonoLimitation(errorMsg)]
  715. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  716. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  717. {
  718. throw new NotSupportedException (errorMsg);
  719. }
  720. [MonoLimitation(errorMsg)]
  721. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  722. Func<IAsyncResult, TResult> endMethod,
  723. object state)
  724. {
  725. throw new NotSupportedException (errorMsg);
  726. }
  727. [MonoLimitation(errorMsg)]
  728. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  729. Func<IAsyncResult, TResult> endMethod,
  730. object state, TaskCreationOptions creationOptions)
  731. {
  732. throw new NotSupportedException (errorMsg);
  733. }
  734. [MonoLimitation(errorMsg)]
  735. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  736. Func<IAsyncResult, TResult> endMethod,
  737. TArg1 arg1, object state)
  738. {
  739. throw new NotSupportedException (errorMsg);
  740. }
  741. [MonoLimitation(errorMsg)]
  742. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  743. Func<IAsyncResult, TResult> endMethod,
  744. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  745. {
  746. throw new NotSupportedException (errorMsg);
  747. }
  748. [MonoLimitation(errorMsg)]
  749. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  750. Func<IAsyncResult, TResult> endMethod,
  751. TArg1 arg1, TArg2 arg2, object state)
  752. {
  753. throw new NotSupportedException (errorMsg);
  754. }
  755. [MonoLimitation(errorMsg)]
  756. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  757. Func<IAsyncResult, TResult> endMethod,
  758. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  759. {
  760. throw new NotSupportedException (errorMsg);
  761. }
  762. [MonoLimitation(errorMsg)]
  763. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  764. Func<IAsyncResult, TResult> endMethod,
  765. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  766. {
  767. throw new NotSupportedException (errorMsg);
  768. }
  769. [MonoLimitation(errorMsg)]
  770. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  771. Func<IAsyncResult, TResult> endMethod,
  772. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  773. TaskCreationOptions creationOptions)
  774. {
  775. throw new NotSupportedException (errorMsg);
  776. }
  777. #endregion
  778. public TaskScheduler Scheduler {
  779. get {
  780. return scheduler;
  781. }
  782. }
  783. public TaskContinuationOptions ContinuationOptions {
  784. get {
  785. return continuationOptions;
  786. }
  787. }
  788. public TaskCreationOptions CreationOptions {
  789. get {
  790. return creationOptions;
  791. }
  792. }
  793. public CancellationToken CancellationToken {
  794. get {
  795. return cancellationToken;
  796. }
  797. }
  798. }
  799. }
  800. #endif