TaskFactory.cs 43 KB

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