2
0

TaskFactory.cs 41 KB

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