TaskFactory.cs 38 KB

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