TaskFactory.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  106. {
  107. Task t = new Task (action, cancellationToken, creationOptions);
  108. //
  109. // Don't start cancelled task it would throw an exception
  110. //
  111. if (!t.IsCompleted)
  112. t.Start (scheduler);
  113. return t;
  114. }
  115. public Task StartNew (Action<object> action, object state)
  116. {
  117. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  118. }
  119. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
  120. {
  121. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  122. }
  123. public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
  124. {
  125. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  126. }
  127. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
  128. TaskScheduler scheduler)
  129. {
  130. Task t = new Task (action, state, cancellationToken, creationOptions);
  131. //
  132. // Don't start cancelled task it would throw an exception
  133. //
  134. if (!t.IsCompleted)
  135. t.Start (scheduler);
  136. return t;
  137. }
  138. #endregion
  139. #region StartNew for Task<TResult>
  140. public Task<TResult> StartNew<TResult> (Func<TResult> function)
  141. {
  142. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  143. }
  144. public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
  145. {
  146. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  147. }
  148. public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
  149. {
  150. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  151. }
  152. public Task<TResult> StartNew<TResult> (Func<TResult> function,
  153. CancellationToken cancellationToken,
  154. TaskCreationOptions creationOptions,
  155. TaskScheduler scheduler)
  156. {
  157. return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  158. }
  159. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
  160. {
  161. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  162. }
  163. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  164. {
  165. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  166. }
  167. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  168. {
  169. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  170. }
  171. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
  172. CancellationToken cancellationToken,
  173. TaskCreationOptions creationOptions,
  174. TaskScheduler scheduler)
  175. {
  176. Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
  177. t.Start (scheduler);
  178. return t;
  179. }
  180. #endregion
  181. #region Continue
  182. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
  183. {
  184. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  185. }
  186. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
  187. {
  188. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  189. }
  190. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  191. {
  192. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  193. }
  194. public Task ContinueWhenAny (Task[] tasks,
  195. Action<Task> continuationAction,
  196. CancellationToken cancellationToken,
  197. TaskContinuationOptions continuationOptions,
  198. TaskScheduler scheduler)
  199. {
  200. var ourTasks = (Task[])tasks.Clone ();
  201. AtomicBoolean trigger = new AtomicBoolean ();
  202. var commonContinuation = new TaskCompletionSource<object> ();
  203. Action<Task> continuationFunc = t => commonContinuation.SetResult (null);
  204. foreach (Task t in ourTasks) {
  205. Task cont = new Task ((o) => continuationAction ((Task)o), t, cancellationToken, creationOptions, t);
  206. t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
  207. cont.ContinueWith (continuationFunc);
  208. }
  209. return commonContinuation.Task;
  210. }
  211. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  212. Action<Task<TAntecedentResult>> continuationAction)
  213. {
  214. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  215. }
  216. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  217. Action<Task<TAntecedentResult>> continuationAction,
  218. CancellationToken cancellationToken)
  219. {
  220. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  221. }
  222. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  223. Action<Task<TAntecedentResult>> continuationAction,
  224. TaskContinuationOptions continuationOptions)
  225. {
  226. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  227. }
  228. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  229. Action<Task<TAntecedentResult>> continuationAction,
  230. CancellationToken cancellationToken,
  231. TaskContinuationOptions continuationOptions,
  232. TaskScheduler scheduler)
  233. {
  234. return ContinueWhenAny ((Task[]) tasks,
  235. (o) => continuationAction ((Task<TAntecedentResult>)o),
  236. cancellationToken, continuationOptions, scheduler);
  237. }
  238. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
  239. {
  240. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  241. }
  242. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  243. Func<Task, TResult> continuationFunction,
  244. CancellationToken cancellationToken)
  245. {
  246. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  247. }
  248. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  249. Func<Task, TResult> continuationFunction,
  250. TaskContinuationOptions continuationOptions)
  251. {
  252. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  253. }
  254. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  255. Func<Task, TResult> continuationFunction,
  256. CancellationToken cancellationToken,
  257. TaskContinuationOptions continuationOptions,
  258. TaskScheduler scheduler)
  259. {
  260. var ourTasks = (Task[])tasks.Clone ();
  261. AtomicBoolean trigger = new AtomicBoolean ();
  262. TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
  263. foreach (Task t in ourTasks) {
  264. Task cont = new Task ((o) => source.SetResult (continuationFunction ((Task)o)), t, cancellationToken, creationOptions, t);
  265. t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
  266. }
  267. return source.Task;
  268. }
  269. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  270. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  271. {
  272. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  273. }
  274. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  275. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  276. CancellationToken cancellationToken)
  277. {
  278. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  279. }
  280. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  281. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  282. TaskContinuationOptions continuationOptions)
  283. {
  284. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  285. }
  286. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  287. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  288. CancellationToken cancellationToken,
  289. TaskContinuationOptions continuationOptions,
  290. TaskScheduler scheduler)
  291. {
  292. return ContinueWhenAny<TResult> ((Task[])tasks,
  293. (t) => continuationFunction((Task<TAntecedentResult>)t),
  294. cancellationToken,
  295. continuationOptions,
  296. scheduler);
  297. }
  298. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
  299. {
  300. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  301. }
  302. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
  303. {
  304. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  305. }
  306. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
  307. TaskContinuationOptions continuationOptions)
  308. {
  309. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  310. }
  311. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
  312. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  313. {
  314. var ourTasks = (Task[])tasks.Clone ();
  315. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  316. Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  317. foreach (Task t in ourTasks)
  318. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  319. return cont;
  320. }
  321. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  322. Action<Task<TAntecedentResult>[]> continuationAction)
  323. {
  324. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  325. }
  326. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  327. Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
  328. {
  329. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  330. }
  331. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  332. TaskContinuationOptions continuationOptions)
  333. {
  334. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  335. }
  336. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  337. Action<Task<TAntecedentResult>[]> continuationAction,
  338. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
  339. TaskScheduler scheduler)
  340. {
  341. return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
  342. continuationOptions, scheduler);
  343. }
  344. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
  345. {
  346. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  347. }
  348. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  349. TaskContinuationOptions continuationOptions)
  350. {
  351. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  352. }
  353. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  354. CancellationToken cancellationToken)
  355. {
  356. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  357. }
  358. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  359. CancellationToken cancellationToken,
  360. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  361. {
  362. var ourTasks = (Task[])tasks.Clone ();
  363. CountdownEvent evt = new CountdownEvent (ourTasks.Length);
  364. Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
  365. foreach (Task t in ourTasks)
  366. t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
  367. return cont;
  368. }
  369. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  370. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  371. {
  372. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  373. }
  374. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  375. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  376. TaskContinuationOptions continuationOptions)
  377. {
  378. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  379. }
  380. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  381. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  382. CancellationToken cancellationToken)
  383. {
  384. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  385. }
  386. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  387. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  388. CancellationToken cancellationToken,
  389. TaskContinuationOptions continuationOptions,
  390. TaskScheduler scheduler)
  391. {
  392. return ContinueWhenAll<TResult> ((Task[]) tasks,
  393. (o) => continuationFunction (tasks),
  394. cancellationToken,
  395. continuationOptions, scheduler);
  396. }
  397. #endregion
  398. #region FromAsync IAsyncResult
  399. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
  400. {
  401. return FromAsync (asyncResult, endMethod, creationOptions);
  402. }
  403. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
  404. {
  405. return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
  406. }
  407. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  408. {
  409. if (endMethod == null)
  410. throw new ArgumentNullException ("endMethod");
  411. return TaskFactory<object>.FromIAsyncResult (asyncResult,
  412. l => {
  413. endMethod (asyncResult);
  414. return null;
  415. }, creationOptions, scheduler);
  416. }
  417. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  418. {
  419. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  420. }
  421. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
  422. {
  423. return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
  424. }
  425. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  426. {
  427. return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
  428. }
  429. #endregion
  430. #region FromAsync Begin/End Method
  431. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
  432. {
  433. return FromAsync (beginMethod, endMethod, state, creationOptions);
  434. }
  435. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  436. object state, TaskCreationOptions creationOptions)
  437. {
  438. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  439. l => { endMethod (l); return null; },
  440. state, creationOptions);
  441. }
  442. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  443. TArg1 arg1, object state)
  444. {
  445. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  446. }
  447. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  448. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  449. {
  450. if (endMethod == null)
  451. throw new ArgumentNullException ("endMethod");
  452. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  453. l => { endMethod (l); return null; },
  454. arg1, state, creationOptions);
  455. }
  456. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  457. Action<IAsyncResult> endMethod,
  458. TArg1 arg1, TArg2 arg2, object state)
  459. {
  460. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  461. }
  462. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  463. Action<IAsyncResult> endMethod,
  464. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  465. {
  466. if (endMethod == null)
  467. throw new ArgumentNullException ("endMethod");
  468. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  469. l => { endMethod (l); return null; },
  470. arg1, arg2, state, creationOptions);
  471. }
  472. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  473. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  474. {
  475. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  476. }
  477. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  478. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  479. {
  480. if (endMethod == null)
  481. throw new ArgumentNullException ("endMethod");
  482. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  483. l => { endMethod (l); return null; },
  484. arg1, arg2, arg3, state, creationOptions);
  485. }
  486. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  487. object state)
  488. {
  489. return FromAsync (beginMethod, endMethod, state, creationOptions);
  490. }
  491. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  492. object state, TaskCreationOptions creationOptions)
  493. {
  494. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
  495. }
  496. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  497. TArg1 arg1, object state)
  498. {
  499. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  500. }
  501. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  502. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  503. {
  504. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
  505. }
  506. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  507. Func<IAsyncResult, TResult> endMethod,
  508. TArg1 arg1, TArg2 arg2, object state)
  509. {
  510. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  511. }
  512. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  513. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  514. {
  515. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  516. }
  517. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  518. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  519. {
  520. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  521. }
  522. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  523. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  524. {
  525. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  526. }
  527. #endregion
  528. TaskScheduler GetScheduler ()
  529. {
  530. return scheduler ?? TaskScheduler.Current;
  531. }
  532. }
  533. }
  534. #endif