TaskFactory.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. #region ctors
  37. public TaskFactory () : this (TaskScheduler.Current, TaskCreationOptions.None, TaskContinuationOptions.None)
  38. {
  39. }
  40. public TaskFactory (TaskScheduler scheduler) : this (scheduler, TaskCreationOptions.None, TaskContinuationOptions.None)
  41. {
  42. }
  43. public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
  44. : this (TaskScheduler.Current, options, contOptions)
  45. {
  46. }
  47. public TaskFactory (TaskScheduler scheduler, TaskCreationOptions options, TaskContinuationOptions contOptions)
  48. {
  49. this.scheduler = scheduler;
  50. this.options = options;
  51. this.contOptions = contOptions;
  52. }
  53. #endregion
  54. #region StartNew for Task
  55. public Task StartNew (Action action)
  56. {
  57. return StartNew (action, options, scheduler);
  58. }
  59. public Task StartNew (Action action, TaskCreationOptions options)
  60. {
  61. return StartNew (action, options, scheduler);
  62. }
  63. public Task StartNew (Action action, TaskCreationOptions options, TaskScheduler scheduler)
  64. {
  65. return StartNew ((o) => action (), null, options, scheduler);
  66. }
  67. public Task StartNew (Action<object> action, object state)
  68. {
  69. return StartNew (action, state, options, scheduler);
  70. }
  71. public Task StartNew (Action<object> action, object state, TaskCreationOptions options)
  72. {
  73. return StartNew (action, state, options, scheduler);
  74. }
  75. public Task StartNew (Action<object> action, object state, TaskCreationOptions options, TaskScheduler scheduler)
  76. {
  77. Task t = new Task (action, state, options);
  78. t.Start (scheduler);
  79. return t;
  80. }
  81. #endregion
  82. #region StartNew for Task<TResult>
  83. public Task<TResult> StartNew<TResult> (Func<TResult> function)
  84. {
  85. return StartNew<TResult> (function, options, scheduler);
  86. }
  87. public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options)
  88. {
  89. return StartNew<TResult> (function, options, scheduler);
  90. }
  91. public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options, TaskScheduler scheduler)
  92. {
  93. return StartNew<TResult> ((o) => function (), null, options, scheduler);
  94. }
  95. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
  96. {
  97. return StartNew<TResult> (function, state, options, scheduler);
  98. }
  99. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options)
  100. {
  101. return StartNew<TResult> (function, state, options, scheduler);
  102. }
  103. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options,
  104. TaskScheduler scheduler)
  105. {
  106. Task<TResult> t = new Task<TResult> (function, state, options);
  107. t.Start (scheduler);
  108. return t;
  109. }
  110. #endregion
  111. #region Continue
  112. [MonoTODO]
  113. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
  114. {
  115. return ContinueWhenAny (tasks, continuationAction, contOptions, scheduler);
  116. }
  117. [MonoTODO]
  118. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  119. {
  120. return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
  121. }
  122. [MonoTODO]
  123. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions,
  124. TaskScheduler scheduler)
  125. {
  126. throw new NotImplementedException ();
  127. }
  128. [MonoTODO]
  129. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction)
  130. {
  131. return ContinueWhenAny (tasks, continuationAction, contOptions);
  132. }
  133. [MonoTODO]
  134. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
  135. TaskContinuationOptions continuationOptions)
  136. {
  137. return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
  138. }
  139. [MonoTODO]
  140. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
  141. TaskContinuationOptions continuationOptions,
  142. TaskScheduler scheduler)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction)
  147. {
  148. return ContinueWhenAll (tasks, continuationFunction, contOptions);
  149. }
  150. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
  151. TaskContinuationOptions continuationOptions)
  152. {
  153. return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
  154. }
  155. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
  156. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  157. {
  158. CountdownEvent evt = new CountdownEvent (tasks.Length);
  159. Task cont = new Task ((o) => continuationFunction ((Task[])o), tasks, options);
  160. foreach (Task t in tasks)
  161. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  162. return cont;
  163. }
  164. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
  165. {
  166. return ContinueWhenAll<TResult> (tasks, continuationFunction, contOptions);
  167. }
  168. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  169. TaskContinuationOptions continuationOptions)
  170. {
  171. return ContinueWhenAll<TResult> (tasks, continuationFunction, continuationOptions, scheduler);
  172. }
  173. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  174. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  175. {
  176. CountdownEvent evt = new CountdownEvent (tasks.Length);
  177. Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), tasks, options);
  178. foreach (Task t in tasks)
  179. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  180. return cont;
  181. }
  182. #endregion
  183. #region FromAsync
  184. // For these methods to work we first have to convert the ThreadPool to use Tasks as it
  185. // is doing in 4.0, then all that is remaining is to identify the Task on which is
  186. // run the async operation (probably with some additional state in a IAsyncResult subclass)
  187. // and call its ContinueWith method accordingly
  188. const string errorMsg = "Mono's thread pool doesn't support this operation yet";
  189. [MonoLimitation(errorMsg)]
  190. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
  191. {
  192. return FromAsync (asyncResult, endMethod, options);
  193. }
  194. [MonoLimitation(errorMsg)]
  195. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
  196. TaskCreationOptions creationOptions)
  197. {
  198. return FromAsync (asyncResult, endMethod, creationOptions);
  199. }
  200. [MonoLimitation(errorMsg)]
  201. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
  202. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  203. {
  204. throw new NotSupportedException (errorMsg);
  205. }
  206. [MonoLimitation(errorMsg)]
  207. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  208. {
  209. return FromAsync<TResult> (asyncResult, endMethod, options);
  210. }
  211. [MonoLimitation(errorMsg)]
  212. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  213. TaskCreationOptions creationOptions)
  214. {
  215. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  216. }
  217. [MonoLimitation(errorMsg)]
  218. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  219. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  220. {
  221. throw new NotSupportedException (errorMsg);
  222. }
  223. [MonoLimitation(errorMsg)]
  224. public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  225. object state)
  226. {
  227. return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, options);
  228. }
  229. [MonoLimitation(errorMsg)]
  230. public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  231. object state, TaskCreationOptions creationOptions)
  232. {
  233. return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
  234. }
  235. [MonoLimitation(errorMsg)]
  236. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  237. TArg1 arg1, object state)
  238. {
  239. throw new NotSupportedException (errorMsg);
  240. }
  241. [MonoLimitation(errorMsg)]
  242. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  243. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  244. {
  245. throw new NotSupportedException (errorMsg);
  246. }
  247. [MonoLimitation(errorMsg)]
  248. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  249. TArg1 arg1, TArg2 arg2, object state)
  250. {
  251. throw new NotSupportedException (errorMsg);
  252. }
  253. [MonoLimitation(errorMsg)]
  254. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  255. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  256. {
  257. throw new NotSupportedException (errorMsg);
  258. }
  259. [MonoLimitation(errorMsg)]
  260. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  261. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  262. {
  263. throw new NotSupportedException (errorMsg);
  264. }
  265. [MonoLimitation(errorMsg)]
  266. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  267. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  268. {
  269. throw new NotSupportedException (errorMsg);
  270. }
  271. [MonoLimitation(errorMsg)]
  272. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  273. Func<IAsyncResult, TResult> endMethod,
  274. object state)
  275. {
  276. throw new NotSupportedException (errorMsg);
  277. }
  278. [MonoLimitation(errorMsg)]
  279. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  280. Func<IAsyncResult, TResult> endMethod,
  281. object state, TaskCreationOptions creationOptions)
  282. {
  283. throw new NotSupportedException (errorMsg);
  284. }
  285. [MonoLimitation(errorMsg)]
  286. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  287. Func<IAsyncResult, TResult> endMethod,
  288. TArg1 arg1, object state)
  289. {
  290. throw new NotSupportedException (errorMsg);
  291. }
  292. [MonoLimitation(errorMsg)]
  293. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  294. Func<IAsyncResult, TResult> endMethod,
  295. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  296. {
  297. throw new NotSupportedException (errorMsg);
  298. }
  299. [MonoLimitation(errorMsg)]
  300. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  301. Func<IAsyncResult, TResult> endMethod,
  302. TArg1 arg1, TArg2 arg2, object state)
  303. {
  304. throw new NotSupportedException (errorMsg);
  305. }
  306. [MonoLimitation(errorMsg)]
  307. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  308. Func<IAsyncResult, TResult> endMethod,
  309. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  310. {
  311. throw new NotSupportedException (errorMsg);
  312. }
  313. [MonoLimitation(errorMsg)]
  314. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  315. Func<IAsyncResult, TResult> endMethod,
  316. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  317. {
  318. throw new NotSupportedException (errorMsg);
  319. }
  320. [MonoLimitation(errorMsg)]
  321. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  322. Func<IAsyncResult, TResult> endMethod,
  323. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  324. TaskCreationOptions creationOptions)
  325. {
  326. throw new NotSupportedException (errorMsg);
  327. }
  328. #endregion
  329. public TaskScheduler Scheduler {
  330. get {
  331. return scheduler;
  332. }
  333. }
  334. public TaskContinuationOptions ContinuationOptions {
  335. get {
  336. return contOptions;
  337. }
  338. }
  339. public TaskCreationOptions CreationOptions {
  340. get {
  341. return options;
  342. }
  343. }
  344. }
  345. public class TaskFactory<TResult>
  346. {
  347. TaskScheduler scheduler;
  348. TaskCreationOptions options;
  349. TaskContinuationOptions contOptions;
  350. TaskFactory parent;
  351. #region ctors
  352. public TaskFactory () : this (TaskScheduler.Current, TaskCreationOptions.None, TaskContinuationOptions.None)
  353. {
  354. }
  355. public TaskFactory (TaskScheduler scheduler) : this (scheduler, TaskCreationOptions.None, TaskContinuationOptions.None)
  356. {
  357. }
  358. public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
  359. : this (TaskScheduler.Current, options, contOptions)
  360. {
  361. }
  362. public TaskFactory (TaskScheduler scheduler, TaskCreationOptions options, TaskContinuationOptions contOptions)
  363. {
  364. this.scheduler = scheduler;
  365. this.options = options;
  366. this.contOptions = contOptions;
  367. this.parent = new TaskFactory (scheduler, options, contOptions);
  368. }
  369. #endregion
  370. #region StartNew for Task<TResult>
  371. public Task<TResult> StartNew (Func<TResult> function)
  372. {
  373. return StartNew (function, options, scheduler);
  374. }
  375. public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options)
  376. {
  377. return StartNew (function, options, scheduler);
  378. }
  379. public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options, TaskScheduler scheduler)
  380. {
  381. return StartNew ((o) => function (), null, options, scheduler);
  382. }
  383. public Task<TResult> StartNew (Func<object, TResult> function, object state)
  384. {
  385. return StartNew (function, state, options, scheduler);
  386. }
  387. public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options)
  388. {
  389. return StartNew (function, state, options, scheduler);
  390. }
  391. public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options,
  392. TaskScheduler scheduler)
  393. {
  394. return parent.StartNew<TResult> (function, state, options, scheduler);
  395. }
  396. #endregion
  397. #region Continue
  398. [MonoTODO]
  399. public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction)
  400. {
  401. return ContinueWhenAny (tasks, continuationAction, contOptions, scheduler);
  402. }
  403. [MonoTODO]
  404. public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction,
  405. TaskContinuationOptions continuationOptions)
  406. {
  407. return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
  408. }
  409. [MonoTODO]
  410. public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction,
  411. TaskContinuationOptions continuationOptions,
  412. TaskScheduler scheduler)
  413. {
  414. throw new NotImplementedException ();
  415. }
  416. [MonoTODO]
  417. public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task<TResult>, TNewResult> continuationAction)
  418. {
  419. return ContinueWhenAny (tasks, continuationAction, contOptions);
  420. }
  421. [MonoTODO]
  422. public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task<TResult>, TNewResult> continuationAction,
  423. TaskContinuationOptions continuationOptions)
  424. {
  425. return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
  426. }
  427. [MonoTODO]
  428. public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task<TResult>, TNewResult> continuationAction,
  429. TaskContinuationOptions continuationOptions,
  430. TaskScheduler scheduler)
  431. {
  432. throw new NotImplementedException ();
  433. }
  434. public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction)
  435. {
  436. return ContinueWhenAll (tasks, continuationFunction, contOptions);
  437. }
  438. public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction,
  439. TaskContinuationOptions continuationOptions)
  440. {
  441. return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
  442. }
  443. public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction,
  444. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  445. {
  446. CountdownEvent evt = new CountdownEvent (tasks.Length);
  447. Task cont = new Task ((o) => continuationFunction ((Task<TResult>[])o), tasks, options);
  448. foreach (Task t in tasks)
  449. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  450. return cont;
  451. }
  452. public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
  453. Func<Task<TResult>[], TNewResult> continuationFunction)
  454. {
  455. return ContinueWhenAll (tasks, continuationFunction, contOptions);
  456. }
  457. public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
  458. Func<Task<TResult>[], TNewResult> continuationFunction,
  459. TaskContinuationOptions continuationOptions)
  460. {
  461. return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
  462. }
  463. public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
  464. Func<Task<TResult>[], TNewResult> continuationFunction,
  465. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  466. {
  467. CountdownEvent evt = new CountdownEvent (tasks.Length);
  468. Task<TNewResult> cont = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>[])o), tasks, options);
  469. foreach (Task t in tasks)
  470. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  471. return cont;
  472. }
  473. #endregion
  474. #region FromAsync
  475. const string errorMsg = "Mono's thread pool doesn't support this operation yet";
  476. [MonoLimitation(errorMsg)]
  477. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  478. {
  479. return FromAsync (asyncResult, endMethod, options);
  480. }
  481. [MonoLimitation(errorMsg)]
  482. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  483. TaskCreationOptions creationOptions)
  484. {
  485. return FromAsync (asyncResult, endMethod, creationOptions);
  486. }
  487. [MonoLimitation(errorMsg)]
  488. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  489. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  490. {
  491. throw new NotSupportedException (errorMsg);
  492. }
  493. [MonoLimitation(errorMsg)]
  494. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  495. Func<IAsyncResult, TResult> endMethod,
  496. object state)
  497. {
  498. throw new NotSupportedException (errorMsg);
  499. }
  500. [MonoLimitation(errorMsg)]
  501. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  502. Func<IAsyncResult, TResult> endMethod,
  503. object state, TaskCreationOptions creationOptions)
  504. {
  505. throw new NotSupportedException (errorMsg);
  506. }
  507. [MonoLimitation(errorMsg)]
  508. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  509. Func<IAsyncResult, TResult> endMethod,
  510. TArg1 arg1, object state)
  511. {
  512. throw new NotSupportedException (errorMsg);
  513. }
  514. [MonoLimitation(errorMsg)]
  515. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  516. Func<IAsyncResult, TResult> endMethod,
  517. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  518. {
  519. throw new NotSupportedException (errorMsg);
  520. }
  521. [MonoLimitation(errorMsg)]
  522. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  523. Func<IAsyncResult, TResult> endMethod,
  524. TArg1 arg1, TArg2 arg2, object state)
  525. {
  526. throw new NotSupportedException (errorMsg);
  527. }
  528. [MonoLimitation(errorMsg)]
  529. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  530. Func<IAsyncResult, TResult> endMethod,
  531. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  532. {
  533. throw new NotSupportedException (errorMsg);
  534. }
  535. [MonoLimitation(errorMsg)]
  536. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  537. Func<IAsyncResult, TResult> endMethod,
  538. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  539. {
  540. throw new NotSupportedException (errorMsg);
  541. }
  542. [MonoLimitation(errorMsg)]
  543. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  544. Func<IAsyncResult, TResult> endMethod,
  545. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  546. TaskCreationOptions creationOptions)
  547. {
  548. throw new NotSupportedException (errorMsg);
  549. }
  550. #endregion
  551. public TaskScheduler Scheduler {
  552. get {
  553. return scheduler;
  554. }
  555. }
  556. public TaskContinuationOptions ContinuationOptions {
  557. get {
  558. return contOptions;
  559. }
  560. }
  561. public TaskCreationOptions CreationOptions {
  562. get {
  563. return options;
  564. }
  565. }
  566. }
  567. }
  568. #endif