TaskFactory.cs 38 KB

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