TaskFactory.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. //
  2. // TaskFactory.cs
  3. //
  4. // Authors:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. // Marek Safar <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in
  19. // all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. #if NET_4_0 || MOBILE
  29. using System;
  30. using System.Threading;
  31. namespace System.Threading.Tasks
  32. {
  33. public class TaskFactory
  34. {
  35. readonly TaskScheduler scheduler;
  36. TaskCreationOptions creationOptions;
  37. TaskContinuationOptions continuationOptions;
  38. CancellationToken cancellationToken;
  39. public TaskFactory ()
  40. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
  41. {
  42. }
  43. public TaskFactory (CancellationToken cancellationToken)
  44. : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
  45. {
  46. }
  47. public TaskFactory (TaskScheduler scheduler)
  48. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  49. {
  50. }
  51. public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  52. : this (CancellationToken.None, creationOptions, continuationOptions, null)
  53. {
  54. }
  55. public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
  56. TaskScheduler scheduler)
  57. {
  58. this.cancellationToken = cancellationToken;
  59. this.scheduler = scheduler;
  60. this.creationOptions = creationOptions;
  61. this.continuationOptions = continuationOptions;
  62. CheckContinuationOptions (continuationOptions);
  63. }
  64. public TaskScheduler Scheduler {
  65. get {
  66. return scheduler;
  67. }
  68. }
  69. public TaskContinuationOptions ContinuationOptions {
  70. get {
  71. return continuationOptions;
  72. }
  73. }
  74. public TaskCreationOptions CreationOptions {
  75. get {
  76. return creationOptions;
  77. }
  78. }
  79. public CancellationToken CancellationToken {
  80. get {
  81. return cancellationToken;
  82. }
  83. }
  84. internal static void CheckContinuationOptions (TaskContinuationOptions continuationOptions)
  85. {
  86. if ((continuationOptions & (TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion)) != 0)
  87. throw new ArgumentOutOfRangeException ("continuationOptions");
  88. const TaskContinuationOptions long_running = TaskContinuationOptions.LongRunning | TaskContinuationOptions.ExecuteSynchronously;
  89. if ((continuationOptions & long_running) == long_running)
  90. throw new ArgumentOutOfRangeException ("continuationOptions", "Synchronous continuations cannot be long running");
  91. }
  92. #region StartNew for Task
  93. public Task StartNew (Action action)
  94. {
  95. return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
  96. }
  97. public Task StartNew (Action action, CancellationToken cancellationToken)
  98. {
  99. return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
  100. }
  101. public Task StartNew (Action action, TaskCreationOptions creationOptions)
  102. {
  103. return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
  104. }
  105. public Task StartNew (Action<object> action, object state)
  106. {
  107. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  108. }
  109. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
  110. {
  111. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  112. }
  113. public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
  114. {
  115. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  116. }
  117. public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  118. {
  119. Task t = new Task (action, cancellationToken, creationOptions);
  120. t.Start (scheduler);
  121. return t;
  122. }
  123. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
  124. TaskScheduler scheduler)
  125. {
  126. Task t = new Task (action, state, cancellationToken, creationOptions);
  127. t.Start (scheduler);
  128. return t;
  129. }
  130. #endregion
  131. #region StartNew for Task<TResult>
  132. public Task<TResult> StartNew<TResult> (Func<TResult> function)
  133. {
  134. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  135. }
  136. public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
  137. {
  138. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  139. }
  140. public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
  141. {
  142. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  143. }
  144. public Task<TResult> StartNew<TResult> (Func<TResult> function,
  145. CancellationToken cancellationToken,
  146. TaskCreationOptions creationOptions,
  147. TaskScheduler scheduler)
  148. {
  149. return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  150. }
  151. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
  152. {
  153. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  154. }
  155. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  156. {
  157. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  158. }
  159. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  160. {
  161. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  162. }
  163. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
  164. CancellationToken cancellationToken,
  165. TaskCreationOptions creationOptions,
  166. TaskScheduler scheduler)
  167. {
  168. Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
  169. t.Start (scheduler);
  170. return t;
  171. }
  172. #endregion
  173. #region Continue
  174. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
  175. {
  176. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  177. }
  178. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
  179. {
  180. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  181. }
  182. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  183. {
  184. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  185. }
  186. public Task ContinueWhenAny (Task[] tasks,
  187. Action<Task> continuationAction,
  188. CancellationToken cancellationToken,
  189. TaskContinuationOptions continuationOptions,
  190. TaskScheduler scheduler)
  191. {
  192. var ourTasks = (Task[])tasks.Clone ();
  193. AtomicBoolean trigger = new AtomicBoolean ();
  194. var commonContinuation = new TaskCompletionSource<object> ();
  195. Action<Task> continuationFunc = t => commonContinuation.SetResult (null);
  196. foreach (Task t in ourTasks) {
  197. Task cont = new Task ((o) => continuationAction ((Task)o), t, cancellationToken, creationOptions, t);
  198. t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
  199. cont.ContinueWith (continuationFunc);
  200. }
  201. return commonContinuation.Task;
  202. }
  203. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  204. Action<Task<TAntecedentResult>> continuationAction)
  205. {
  206. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  207. }
  208. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  209. Action<Task<TAntecedentResult>> continuationAction,
  210. CancellationToken cancellationToken)
  211. {
  212. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  213. }
  214. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  215. Action<Task<TAntecedentResult>> continuationAction,
  216. TaskContinuationOptions continuationOptions)
  217. {
  218. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  219. }
  220. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  221. Action<Task<TAntecedentResult>> continuationAction,
  222. CancellationToken cancellationToken,
  223. TaskContinuationOptions continuationOptions,
  224. TaskScheduler scheduler)
  225. {
  226. return ContinueWhenAny ((Task[]) tasks,
  227. (o) => continuationAction ((Task<TAntecedentResult>)o),
  228. cancellationToken, continuationOptions, scheduler);
  229. }
  230. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
  231. {
  232. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  233. }
  234. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  235. Func<Task, TResult> continuationFunction,
  236. CancellationToken cancellationToken)
  237. {
  238. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  239. }
  240. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  241. Func<Task, TResult> continuationFunction,
  242. TaskContinuationOptions continuationOptions)
  243. {
  244. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  245. }
  246. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  247. Func<Task, TResult> continuationFunction,
  248. CancellationToken cancellationToken,
  249. TaskContinuationOptions continuationOptions,
  250. TaskScheduler scheduler)
  251. {
  252. var ourTasks = (Task[])tasks.Clone ();
  253. AtomicBoolean trigger = new AtomicBoolean ();
  254. TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
  255. foreach (Task t in ourTasks) {
  256. Task cont = new Task ((o) => source.SetResult (continuationFunction ((Task)o)), t, cancellationToken, creationOptions, t);
  257. t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
  258. }
  259. return source.Task;
  260. }
  261. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  262. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  263. {
  264. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  265. }
  266. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  267. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  268. CancellationToken cancellationToken)
  269. {
  270. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  271. }
  272. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  273. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  274. TaskContinuationOptions continuationOptions)
  275. {
  276. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  277. }
  278. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  279. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  280. CancellationToken cancellationToken,
  281. TaskContinuationOptions continuationOptions,
  282. TaskScheduler scheduler)
  283. {
  284. return ContinueWhenAny<TResult> ((Task[])tasks,
  285. (t) => continuationFunction((Task<TAntecedentResult>)t),
  286. cancellationToken,
  287. continuationOptions,
  288. scheduler);
  289. }
  290. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
  291. {
  292. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  293. }
  294. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
  295. {
  296. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  297. }
  298. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
  299. TaskContinuationOptions continuationOptions)
  300. {
  301. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  302. }
  303. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
  304. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  305. {
  306. var ourTasks = (Task[])tasks.Clone ();
  307. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  308. Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  309. foreach (Task t in ourTasks)
  310. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  311. return cont;
  312. }
  313. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  314. Action<Task<TAntecedentResult>[]> continuationAction)
  315. {
  316. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  317. }
  318. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  319. Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
  320. {
  321. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  322. }
  323. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  324. TaskContinuationOptions continuationOptions)
  325. {
  326. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  327. }
  328. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  329. Action<Task<TAntecedentResult>[]> continuationAction,
  330. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
  331. TaskScheduler scheduler)
  332. {
  333. return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
  334. continuationOptions, scheduler);
  335. }
  336. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
  337. {
  338. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  339. }
  340. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  341. TaskContinuationOptions continuationOptions)
  342. {
  343. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  344. }
  345. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  346. CancellationToken cancellationToken)
  347. {
  348. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  349. }
  350. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  351. CancellationToken cancellationToken,
  352. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  353. {
  354. var ourTasks = (Task[])tasks.Clone ();
  355. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  356. Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  357. foreach (Task t in ourTasks)
  358. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  359. return cont;
  360. }
  361. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  362. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  363. {
  364. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  365. }
  366. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  367. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  368. TaskContinuationOptions continuationOptions)
  369. {
  370. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  371. }
  372. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  373. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  374. CancellationToken cancellationToken)
  375. {
  376. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  377. }
  378. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  379. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  380. CancellationToken cancellationToken,
  381. TaskContinuationOptions continuationOptions,
  382. TaskScheduler scheduler)
  383. {
  384. return ContinueWhenAll<TResult> ((Task[]) tasks,
  385. (o) => continuationFunction (tasks),
  386. cancellationToken,
  387. continuationOptions, scheduler);
  388. }
  389. #endregion
  390. #region FromAsync IAsyncResult
  391. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
  392. {
  393. return FromAsync (asyncResult, endMethod, creationOptions);
  394. }
  395. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
  396. {
  397. return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
  398. }
  399. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  400. {
  401. if (endMethod == null)
  402. throw new ArgumentNullException ("endMethod");
  403. return TaskFactory<object>.FromIAsyncResult (asyncResult,
  404. l => {
  405. endMethod (asyncResult);
  406. return null;
  407. }, creationOptions, scheduler);
  408. }
  409. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  410. {
  411. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  412. }
  413. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
  414. {
  415. return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
  416. }
  417. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  418. {
  419. return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
  420. }
  421. #endregion
  422. #region FromAsync Begin/End Method
  423. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
  424. {
  425. return FromAsync (beginMethod, endMethod, state, creationOptions);
  426. }
  427. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  428. object state, TaskCreationOptions creationOptions)
  429. {
  430. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  431. l => { endMethod (l); return null; },
  432. state, creationOptions);
  433. }
  434. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  435. TArg1 arg1, object state)
  436. {
  437. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  438. }
  439. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  440. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  441. {
  442. if (endMethod == null)
  443. throw new ArgumentNullException ("endMethod");
  444. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  445. l => { endMethod (l); return null; },
  446. arg1, state, creationOptions);
  447. }
  448. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  449. Action<IAsyncResult> endMethod,
  450. TArg1 arg1, TArg2 arg2, object state)
  451. {
  452. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  453. }
  454. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  455. Action<IAsyncResult> endMethod,
  456. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  457. {
  458. if (endMethod == null)
  459. throw new ArgumentNullException ("endMethod");
  460. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  461. l => { endMethod (l); return null; },
  462. arg1, arg2, state, creationOptions);
  463. }
  464. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  465. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  466. {
  467. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  468. }
  469. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  470. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  471. {
  472. if (endMethod == null)
  473. throw new ArgumentNullException ("endMethod");
  474. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  475. l => { endMethod (l); return null; },
  476. arg1, arg2, arg3, state, creationOptions);
  477. }
  478. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  479. object state)
  480. {
  481. return FromAsync (beginMethod, endMethod, state, creationOptions);
  482. }
  483. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  484. object state, TaskCreationOptions creationOptions)
  485. {
  486. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
  487. }
  488. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  489. TArg1 arg1, object state)
  490. {
  491. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  492. }
  493. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  494. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  495. {
  496. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
  497. }
  498. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  499. Func<IAsyncResult, TResult> endMethod,
  500. TArg1 arg1, TArg2 arg2, object state)
  501. {
  502. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  503. }
  504. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  505. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  506. {
  507. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  508. }
  509. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  510. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  511. {
  512. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  513. }
  514. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  515. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  516. {
  517. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  518. }
  519. #endregion
  520. TaskScheduler GetScheduler ()
  521. {
  522. return scheduler ?? TaskScheduler.Current;
  523. }
  524. }
  525. }
  526. #endif