TaskFactory.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 || BOOTSTRAP_NET_4_0
  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. return StartNew ((o) => action (), null, cancellationToken, creationOptions, scheduler);
  91. }
  92. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
  93. TaskScheduler scheduler)
  94. {
  95. Task t = new Task (action, state, cancellationToken, creationOptions);
  96. t.Start (scheduler);
  97. return t;
  98. }
  99. #endregion
  100. #region StartNew for Task<TResult>
  101. public Task<TResult> StartNew<TResult> (Func<TResult> function)
  102. {
  103. return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
  104. }
  105. public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
  106. {
  107. return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
  108. }
  109. public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
  110. {
  111. return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
  112. }
  113. public Task<TResult> StartNew<TResult> (Func<TResult> function,
  114. CancellationToken cancellationToken,
  115. TaskCreationOptions creationOptions,
  116. TaskScheduler scheduler)
  117. {
  118. return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  119. }
  120. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
  121. {
  122. return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  123. }
  124. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  125. {
  126. return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  127. }
  128. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  129. {
  130. return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  131. }
  132. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
  133. CancellationToken cancellationToken,
  134. TaskCreationOptions creationOptions,
  135. TaskScheduler scheduler)
  136. {
  137. Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
  138. t.Start (scheduler);
  139. return t;
  140. }
  141. #endregion
  142. #region Continue
  143. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
  144. {
  145. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  146. }
  147. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
  148. {
  149. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  150. }
  151. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  152. {
  153. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  154. }
  155. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken,
  156. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  157. {
  158. var ourTasks = (Task[])tasks.Clone ();
  159. AtomicBoolean trigger = new AtomicBoolean ();
  160. Task commonContinuation = new Task (null);
  161. foreach (Task t in ourTasks) {
  162. Task cont = new Task ((o) => { continuationAction ((Task)o); }, t, cancellationToken, creationOptions);
  163. t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
  164. cont.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
  165. }
  166. return commonContinuation;
  167. }
  168. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction)
  169. {
  170. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  171. }
  172. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
  173. CancellationToken cancellationToken)
  174. {
  175. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  176. }
  177. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
  178. TaskContinuationOptions continuationOptions)
  179. {
  180. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  181. }
  182. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
  183. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
  184. TaskScheduler scheduler)
  185. {
  186. return ContinueWhenAny ((Task[]) tasks, (o) => continuationAction ((Task<TAntecedentResult>)o),
  187. cancellationToken, continuationOptions, scheduler);
  188. }
  189. [MonoTODO]
  190. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
  191. {
  192. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  193. }
  194. [MonoTODO]
  195. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction,
  196. CancellationToken cancellationToken)
  197. {
  198. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  199. }
  200. [MonoTODO]
  201. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction,
  202. TaskContinuationOptions continuationOptions)
  203. {
  204. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  205. }
  206. [MonoTODO]
  207. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction,
  208. CancellationToken cancellationToken,
  209. TaskContinuationOptions continuationOptions,
  210. TaskScheduler scheduler)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. [MonoTODO]
  215. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  216. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  217. {
  218. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  219. }
  220. [MonoTODO]
  221. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  222. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  223. CancellationToken cancellationToken)
  224. {
  225. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  226. }
  227. [MonoTODO]
  228. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  229. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  230. TaskContinuationOptions continuationOptions)
  231. {
  232. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  233. }
  234. [MonoTODO]
  235. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  236. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  237. CancellationToken cancellationToken,
  238. TaskContinuationOptions continuationOptions,
  239. TaskScheduler scheduler)
  240. {
  241. return ContinueWhenAny<TResult> ((Task[])tasks, (t) => continuationFunction((Task<TAntecedentResult>)t), cancellationToken, continuationOptions, scheduler);
  242. }
  243. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
  244. {
  245. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  246. }
  247. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
  248. {
  249. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  250. }
  251. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
  252. TaskContinuationOptions continuationOptions)
  253. {
  254. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  255. }
  256. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
  257. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  258. {
  259. var ourTasks = (Task[])tasks.Clone ();
  260. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  261. Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  262. foreach (Task t in ourTasks)
  263. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  264. return cont;
  265. }
  266. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  267. Action<Task<TAntecedentResult>[]> continuationAction)
  268. {
  269. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  270. }
  271. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  272. Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
  273. {
  274. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  275. }
  276. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  277. TaskContinuationOptions continuationOptions)
  278. {
  279. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
  280. }
  281. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  282. Action<Task<TAntecedentResult>[]> continuationAction,
  283. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
  284. TaskScheduler scheduler)
  285. {
  286. return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
  287. continuationOptions, scheduler);
  288. }
  289. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
  290. {
  291. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  292. }
  293. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  294. TaskContinuationOptions continuationOptions)
  295. {
  296. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  297. }
  298. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  299. CancellationToken cancellationToken)
  300. {
  301. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  302. }
  303. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  304. CancellationToken cancellationToken,
  305. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  306. {
  307. var ourTasks = (Task[])tasks.Clone ();
  308. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  309. Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  310. foreach (Task t in ourTasks)
  311. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  312. return cont;
  313. }
  314. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  315. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  316. {
  317. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  318. }
  319. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  320. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  321. TaskContinuationOptions continuationOptions)
  322. {
  323. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  324. }
  325. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  326. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  327. CancellationToken cancellationToken)
  328. {
  329. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  330. }
  331. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  332. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  333. CancellationToken cancellationToken,
  334. TaskContinuationOptions continuationOptions,
  335. TaskScheduler scheduler)
  336. {
  337. return ContinueWhenAll<TResult> ((Task[]) tasks,
  338. (o) => continuationFunction (tasks),
  339. cancellationToken,
  340. continuationOptions, scheduler);
  341. }
  342. #endregion
  343. #region FromAsync
  344. // For these methods to work we first have to convert the ThreadPool to use Tasks as it
  345. // is doing in 4.0, then all that is remaining is to identify the Task on which is
  346. // run the async operation (probably with some additional state in a IAsyncResult subclass)
  347. // and call its ContinueWith method accordingly
  348. const string errorMsg = "Mono's thread pool doesn't support this operation yet";
  349. [MonoLimitation(errorMsg)]
  350. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
  351. {
  352. return FromAsync (asyncResult, endMethod, creationOptions);
  353. }
  354. [MonoLimitation(errorMsg)]
  355. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
  356. TaskCreationOptions creationOptions)
  357. {
  358. return FromAsync (asyncResult, endMethod, creationOptions);
  359. }
  360. [MonoLimitation(errorMsg)]
  361. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
  362. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  363. {
  364. throw new NotSupportedException (errorMsg);
  365. }
  366. [MonoLimitation(errorMsg)]
  367. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  368. {
  369. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  370. }
  371. [MonoLimitation(errorMsg)]
  372. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  373. TaskCreationOptions creationOptions)
  374. {
  375. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  376. }
  377. [MonoLimitation(errorMsg)]
  378. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  379. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  380. {
  381. throw new NotSupportedException (errorMsg);
  382. }
  383. [MonoLimitation(errorMsg)]
  384. public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  385. object state)
  386. {
  387. return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
  388. }
  389. [MonoLimitation(errorMsg)]
  390. public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  391. object state, TaskCreationOptions creationOptions)
  392. {
  393. return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
  394. }
  395. [MonoLimitation(errorMsg)]
  396. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  397. TArg1 arg1, object state)
  398. {
  399. throw new NotSupportedException (errorMsg);
  400. }
  401. [MonoLimitation(errorMsg)]
  402. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  403. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  404. {
  405. throw new NotSupportedException (errorMsg);
  406. }
  407. [MonoLimitation(errorMsg)]
  408. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  409. TArg1 arg1, TArg2 arg2, object state)
  410. {
  411. throw new NotSupportedException (errorMsg);
  412. }
  413. [MonoLimitation(errorMsg)]
  414. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  415. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  416. {
  417. throw new NotSupportedException (errorMsg);
  418. }
  419. [MonoLimitation(errorMsg)]
  420. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  421. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  422. {
  423. throw new NotSupportedException (errorMsg);
  424. }
  425. [MonoLimitation(errorMsg)]
  426. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  427. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  428. {
  429. throw new NotSupportedException (errorMsg);
  430. }
  431. [MonoLimitation(errorMsg)]
  432. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  433. Func<IAsyncResult, TResult> endMethod,
  434. object state)
  435. {
  436. throw new NotSupportedException (errorMsg);
  437. }
  438. [MonoLimitation(errorMsg)]
  439. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  440. Func<IAsyncResult, TResult> endMethod,
  441. object state, TaskCreationOptions creationOptions)
  442. {
  443. throw new NotSupportedException (errorMsg);
  444. }
  445. [MonoLimitation(errorMsg)]
  446. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  447. Func<IAsyncResult, TResult> endMethod,
  448. TArg1 arg1, object state)
  449. {
  450. throw new NotSupportedException (errorMsg);
  451. }
  452. [MonoLimitation(errorMsg)]
  453. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  454. Func<IAsyncResult, TResult> endMethod,
  455. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  456. {
  457. throw new NotSupportedException (errorMsg);
  458. }
  459. [MonoLimitation(errorMsg)]
  460. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  461. Func<IAsyncResult, TResult> endMethod,
  462. TArg1 arg1, TArg2 arg2, object state)
  463. {
  464. throw new NotSupportedException (errorMsg);
  465. }
  466. [MonoLimitation(errorMsg)]
  467. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  468. Func<IAsyncResult, TResult> endMethod,
  469. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  470. {
  471. throw new NotSupportedException (errorMsg);
  472. }
  473. [MonoLimitation(errorMsg)]
  474. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  475. Func<IAsyncResult, TResult> endMethod,
  476. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  477. {
  478. throw new NotSupportedException (errorMsg);
  479. }
  480. [MonoLimitation(errorMsg)]
  481. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  482. Func<IAsyncResult, TResult> endMethod,
  483. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  484. TaskCreationOptions creationOptions)
  485. {
  486. throw new NotSupportedException (errorMsg);
  487. }
  488. #endregion
  489. public TaskScheduler Scheduler {
  490. get {
  491. return scheduler;
  492. }
  493. }
  494. public TaskContinuationOptions ContinuationOptions {
  495. get {
  496. return continuationOptions;
  497. }
  498. }
  499. public TaskCreationOptions CreationOptions {
  500. get {
  501. return creationOptions;
  502. }
  503. }
  504. public CancellationToken CancellationToken {
  505. get {
  506. return cancellationToken;
  507. }
  508. }
  509. }
  510. public class TaskFactory<TResult>
  511. {
  512. TaskScheduler scheduler;
  513. TaskCreationOptions creationOptions;
  514. TaskContinuationOptions continuationOptions;
  515. CancellationToken cancellationToken;
  516. TaskFactory parent;
  517. #region ctors
  518. public TaskFactory ()
  519. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  520. {
  521. }
  522. public TaskFactory (TaskScheduler scheduler)
  523. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  524. {
  525. }
  526. public TaskFactory (CancellationToken cancellationToken)
  527. : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  528. {
  529. }
  530. public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  531. : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
  532. {
  533. }
  534. public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
  535. TaskScheduler scheduler)
  536. {
  537. this.cancellationToken = cancellationToken;
  538. this.scheduler = scheduler;
  539. this.creationOptions = creationOptions;
  540. this.continuationOptions = continuationOptions;
  541. this.parent = new TaskFactory (cancellationToken, creationOptions, continuationOptions, scheduler);
  542. }
  543. #endregion
  544. #region StartNew for Task<TResult>
  545. public Task<TResult> StartNew (Func<TResult> function)
  546. {
  547. return StartNew (function, cancellationToken, creationOptions, scheduler);
  548. }
  549. public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
  550. {
  551. return StartNew (function, cancellationToken, creationOptions, scheduler);
  552. }
  553. public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
  554. {
  555. return StartNew (function, cancellationToken, creationOptions, scheduler);
  556. }
  557. public Task<TResult> StartNew (Func<TResult> function,
  558. CancellationToken cancellationToken,
  559. TaskCreationOptions creationOptions,
  560. TaskScheduler scheduler)
  561. {
  562. return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  563. }
  564. public Task<TResult> StartNew (Func<object, TResult> function, object state)
  565. {
  566. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  567. }
  568. public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  569. {
  570. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  571. }
  572. public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  573. {
  574. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  575. }
  576. public Task<TResult> StartNew (Func<object, TResult> function, object state,
  577. CancellationToken cancellationToken,
  578. TaskCreationOptions creationOptions,
  579. TaskScheduler scheduler)
  580. {
  581. return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  582. }
  583. #endregion
  584. #region Continue
  585. [MonoTODO]
  586. public Task<TResult> ContinueWhenAny (Task[] tasks,
  587. Func<Task, TResult> continuationFunction)
  588. {
  589. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  590. }
  591. [MonoTODO]
  592. public Task<TResult> ContinueWhenAny (Task[] tasks,
  593. Func<Task, TResult> continuationFunction,
  594. CancellationToken cancellationToken)
  595. {
  596. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  597. }
  598. [MonoTODO]
  599. public Task<TResult> ContinueWhenAny (Task[] tasks,
  600. Func<Task, TResult> continuationFunction,
  601. TaskContinuationOptions continuationOptions)
  602. {
  603. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  604. }
  605. [MonoTODO]
  606. public Task<TResult> ContinueWhenAny (Task[] tasks,
  607. Func<Task, TResult> continuationFunction,
  608. CancellationToken cancellationToken,
  609. TaskContinuationOptions continuationOptions,
  610. TaskScheduler scheduler)
  611. {
  612. throw new NotImplementedException ();
  613. }
  614. [MonoTODO]
  615. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  616. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  617. {
  618. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  619. }
  620. [MonoTODO]
  621. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  622. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  623. CancellationToken cancellationToken)
  624. {
  625. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  626. }
  627. [MonoTODO]
  628. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  629. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  630. TaskContinuationOptions continuationOptions)
  631. {
  632. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  633. }
  634. [MonoTODO]
  635. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  636. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  637. CancellationToken cancellationToken,
  638. TaskContinuationOptions continuationOptions,
  639. TaskScheduler scheduler)
  640. {
  641. throw new NotImplementedException ();
  642. }
  643. public Task<TResult> ContinueWhenAll (Task[] tasks,
  644. Func<Task[], TResult> continuationFunction)
  645. {
  646. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  647. }
  648. public Task<TResult> ContinueWhenAll (Task[] tasks,
  649. Func<Task[], TResult> continuationFunction,
  650. TaskContinuationOptions continuationOptions)
  651. {
  652. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  653. }
  654. public Task<TResult> ContinueWhenAll (Task[] tasks,
  655. Func<Task[], TResult> continuationFunction,
  656. CancellationToken cancellationToken)
  657. {
  658. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  659. }
  660. public Task<TResult> ContinueWhenAll (Task[] tasks,
  661. Func<Task[], TResult> continuationFunction,
  662. CancellationToken cancellationToken,
  663. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  664. {
  665. var ourTasks = (Task[])tasks.Clone ();
  666. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  667. Task<TResult> cont = new Task<TResult> ((o) => continuationFunction (ourTasks), ourTasks, cancellationToken, creationOptions);
  668. foreach (Task t in ourTasks)
  669. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  670. return cont;
  671. }
  672. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  673. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  674. {
  675. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  676. }
  677. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  678. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  679. TaskContinuationOptions continuationOptions)
  680. {
  681. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  682. }
  683. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  684. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  685. CancellationToken cancellationToken)
  686. {
  687. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  688. }
  689. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  690. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  691. CancellationToken cancellationToken,
  692. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  693. {
  694. var ourTasks = (Task<TAntecedentResult>[])tasks.Clone ();
  695. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  696. Task<TResult> cont = new Task<TResult> ((o) => continuationFunction (ourTasks), ourTasks, cancellationToken, creationOptions);
  697. foreach (Task t in ourTasks)
  698. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  699. return cont;
  700. }
  701. #endregion
  702. #region FromAsync
  703. const string errorMsg = "Mono's thread pool doesn't support this operation yet";
  704. [MonoLimitation(errorMsg)]
  705. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  706. {
  707. return FromAsync (asyncResult, endMethod, creationOptions);
  708. }
  709. [MonoLimitation(errorMsg)]
  710. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  711. TaskCreationOptions creationOptions)
  712. {
  713. return FromAsync (asyncResult, endMethod, creationOptions);
  714. }
  715. [MonoLimitation(errorMsg)]
  716. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  717. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  718. {
  719. throw new NotSupportedException (errorMsg);
  720. }
  721. [MonoLimitation(errorMsg)]
  722. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  723. Func<IAsyncResult, TResult> endMethod,
  724. object state)
  725. {
  726. throw new NotSupportedException (errorMsg);
  727. }
  728. [MonoLimitation(errorMsg)]
  729. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  730. Func<IAsyncResult, TResult> endMethod,
  731. object state, TaskCreationOptions creationOptions)
  732. {
  733. throw new NotSupportedException (errorMsg);
  734. }
  735. [MonoLimitation(errorMsg)]
  736. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  737. Func<IAsyncResult, TResult> endMethod,
  738. TArg1 arg1, object state)
  739. {
  740. throw new NotSupportedException (errorMsg);
  741. }
  742. [MonoLimitation(errorMsg)]
  743. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  744. Func<IAsyncResult, TResult> endMethod,
  745. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  746. {
  747. throw new NotSupportedException (errorMsg);
  748. }
  749. [MonoLimitation(errorMsg)]
  750. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  751. Func<IAsyncResult, TResult> endMethod,
  752. TArg1 arg1, TArg2 arg2, object state)
  753. {
  754. throw new NotSupportedException (errorMsg);
  755. }
  756. [MonoLimitation(errorMsg)]
  757. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  758. Func<IAsyncResult, TResult> endMethod,
  759. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  760. {
  761. throw new NotSupportedException (errorMsg);
  762. }
  763. [MonoLimitation(errorMsg)]
  764. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  765. Func<IAsyncResult, TResult> endMethod,
  766. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  767. {
  768. throw new NotSupportedException (errorMsg);
  769. }
  770. [MonoLimitation(errorMsg)]
  771. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  772. Func<IAsyncResult, TResult> endMethod,
  773. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  774. TaskCreationOptions creationOptions)
  775. {
  776. throw new NotSupportedException (errorMsg);
  777. }
  778. #endregion
  779. public TaskScheduler Scheduler {
  780. get {
  781. return scheduler;
  782. }
  783. }
  784. public TaskContinuationOptions ContinuationOptions {
  785. get {
  786. return continuationOptions;
  787. }
  788. }
  789. public TaskCreationOptions CreationOptions {
  790. get {
  791. return creationOptions;
  792. }
  793. }
  794. public CancellationToken CancellationToken {
  795. get {
  796. return cancellationToken;
  797. }
  798. }
  799. }
  800. }
  801. #endif