TaskFactory.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. namespace System.Threading.Tasks
  30. {
  31. public class TaskFactory
  32. {
  33. readonly TaskScheduler scheduler;
  34. TaskCreationOptions creationOptions;
  35. TaskContinuationOptions continuationOptions;
  36. CancellationToken cancellationToken;
  37. public TaskFactory ()
  38. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
  39. {
  40. }
  41. public TaskFactory (CancellationToken cancellationToken)
  42. : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
  43. {
  44. }
  45. public TaskFactory (TaskScheduler scheduler)
  46. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  47. {
  48. }
  49. public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  50. : this (CancellationToken.None, creationOptions, continuationOptions, null)
  51. {
  52. }
  53. public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
  54. TaskScheduler scheduler)
  55. {
  56. this.cancellationToken = cancellationToken;
  57. this.scheduler = scheduler;
  58. this.creationOptions = creationOptions;
  59. this.continuationOptions = continuationOptions;
  60. CheckContinuationOptions (continuationOptions);
  61. }
  62. public TaskScheduler Scheduler {
  63. get {
  64. return scheduler;
  65. }
  66. }
  67. public TaskContinuationOptions ContinuationOptions {
  68. get {
  69. return continuationOptions;
  70. }
  71. }
  72. public TaskCreationOptions CreationOptions {
  73. get {
  74. return creationOptions;
  75. }
  76. }
  77. public CancellationToken CancellationToken {
  78. get {
  79. return cancellationToken;
  80. }
  81. }
  82. internal static void CheckContinuationOptions (TaskContinuationOptions continuationOptions)
  83. {
  84. if ((continuationOptions & (TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion)) != 0)
  85. throw new ArgumentOutOfRangeException ("continuationOptions");
  86. const TaskContinuationOptions long_running = TaskContinuationOptions.LongRunning | TaskContinuationOptions.ExecuteSynchronously;
  87. if ((continuationOptions & long_running) == long_running)
  88. throw new ArgumentOutOfRangeException ("continuationOptions", "Synchronous continuations cannot be long running");
  89. }
  90. #region StartNew for Task
  91. public Task StartNew (Action action)
  92. {
  93. return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
  94. }
  95. public Task StartNew (Action action, CancellationToken cancellationToken)
  96. {
  97. return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
  98. }
  99. public Task StartNew (Action action, TaskCreationOptions creationOptions)
  100. {
  101. return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
  102. }
  103. public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  104. {
  105. Task t = new Task (action, cancellationToken, creationOptions);
  106. //
  107. // Don't start cancelled task it would throw an exception
  108. //
  109. if (!t.IsCompleted)
  110. t.Start (scheduler);
  111. return t;
  112. }
  113. public Task StartNew (Action<object> action, object state)
  114. {
  115. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  116. }
  117. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
  118. {
  119. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  120. }
  121. public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
  122. {
  123. return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
  124. }
  125. public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
  126. TaskScheduler scheduler)
  127. {
  128. Task t = new Task (action, state, cancellationToken, creationOptions);
  129. //
  130. // Don't start cancelled task it would throw an exception
  131. //
  132. if (!t.IsCompleted)
  133. t.Start (scheduler);
  134. return t;
  135. }
  136. #endregion
  137. #region StartNew for Task<TResult>
  138. public Task<TResult> StartNew<TResult> (Func<TResult> function)
  139. {
  140. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  141. }
  142. public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
  143. {
  144. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  145. }
  146. public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
  147. {
  148. return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
  149. }
  150. public Task<TResult> StartNew<TResult> (Func<TResult> function,
  151. CancellationToken cancellationToken,
  152. TaskCreationOptions creationOptions,
  153. TaskScheduler scheduler)
  154. {
  155. var t = new Task<TResult> (function, cancellationToken, creationOptions);
  156. //
  157. // Don't start cancelled task it would throw an exception
  158. //
  159. if (!t.IsCompleted)
  160. t.Start (scheduler);
  161. return t;
  162. }
  163. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
  164. {
  165. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  166. }
  167. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  168. {
  169. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  170. }
  171. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  172. {
  173. return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
  174. }
  175. public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
  176. CancellationToken cancellationToken,
  177. TaskCreationOptions creationOptions,
  178. TaskScheduler scheduler)
  179. {
  180. Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
  181. t.Start (scheduler);
  182. return t;
  183. }
  184. #endregion
  185. #region Continue
  186. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
  187. {
  188. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  189. }
  190. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
  191. {
  192. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  193. }
  194. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
  195. {
  196. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  197. }
  198. public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  199. {
  200. if (tasks == null)
  201. throw new ArgumentNullException ("tasks");
  202. if (tasks.Length == 0)
  203. throw new ArgumentException ("The tasks argument contains no tasks", "tasks");
  204. foreach (var ta in tasks) {
  205. if (ta == null)
  206. throw new ArgumentException ("The tasks argument constains a null value", "tasks");
  207. }
  208. if (continuationAction == null)
  209. throw new ArgumentNullException ("continuationAction");
  210. var t = new Task<int> (l => {
  211. var data = (Tuple<Task[], CancellationToken>) l;
  212. return Task.WaitAny (data.Item1, data.Item2);
  213. }, Tuple.Create (tasks, cancellationToken));
  214. var cont = t.ContinueWith (TaskActionInvoker.Create (continuationAction, tasks), cancellationToken, continuationOptions, scheduler);
  215. t.Start (scheduler);
  216. return cont;
  217. }
  218. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  219. Action<Task<TAntecedentResult>> continuationAction)
  220. {
  221. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  222. }
  223. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  224. Action<Task<TAntecedentResult>> continuationAction,
  225. CancellationToken cancellationToken)
  226. {
  227. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  228. }
  229. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  230. Action<Task<TAntecedentResult>> continuationAction,
  231. TaskContinuationOptions continuationOptions)
  232. {
  233. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  234. }
  235. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  236. Action<Task<TAntecedentResult>> continuationAction,
  237. CancellationToken cancellationToken,
  238. TaskContinuationOptions continuationOptions,
  239. TaskScheduler scheduler)
  240. {
  241. return ContinueWhenAny ((Task[]) tasks,
  242. (o) => continuationAction ((Task<TAntecedentResult>)o),
  243. cancellationToken, continuationOptions, scheduler);
  244. }
  245. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
  246. {
  247. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  248. }
  249. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  250. Func<Task, TResult> continuationFunction,
  251. CancellationToken cancellationToken)
  252. {
  253. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  254. }
  255. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  256. Func<Task, TResult> continuationFunction,
  257. TaskContinuationOptions continuationOptions)
  258. {
  259. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  260. }
  261. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  262. Func<Task, TResult> continuationFunction,
  263. CancellationToken cancellationToken,
  264. TaskContinuationOptions continuationOptions,
  265. TaskScheduler scheduler)
  266. {
  267. var t = new Task<int> (l => {
  268. var data = (Tuple<Task[], CancellationToken>) l;
  269. return Task.WaitAny (data.Item1, data.Item2);
  270. }, Tuple.Create (tasks, cancellationToken));
  271. var cont = t.ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
  272. t.Start (scheduler);
  273. return cont;
  274. }
  275. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  276. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  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. CancellationToken cancellationToken)
  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. TaskContinuationOptions continuationOptions)
  289. {
  290. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  291. }
  292. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  293. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  294. CancellationToken cancellationToken,
  295. TaskContinuationOptions continuationOptions,
  296. TaskScheduler scheduler)
  297. {
  298. return ContinueWhenAny<TResult> ((Task[])tasks,
  299. (t) => continuationFunction((Task<TAntecedentResult>)t),
  300. cancellationToken,
  301. continuationOptions,
  302. scheduler);
  303. }
  304. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
  305. {
  306. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  307. }
  308. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
  309. {
  310. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  311. }
  312. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
  313. TaskContinuationOptions continuationOptions)
  314. {
  315. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  316. }
  317. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
  318. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  319. {
  320. var t = new Task (l => {
  321. var data = (Tuple<Task[], CancellationToken>) l;
  322. Task.WaitAll (data.Item1, data.Item2);
  323. }, Tuple.Create (tasks, cancellationToken));
  324. var cont = t.ContinueWith (TaskActionInvoker.Create (continuationAction, tasks), cancellationToken, continuationOptions, scheduler);
  325. t.Start (scheduler);
  326. return cont;
  327. }
  328. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  329. Action<Task<TAntecedentResult>[]> continuationAction)
  330. {
  331. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  332. }
  333. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  334. Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
  335. {
  336. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  337. }
  338. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  339. TaskContinuationOptions continuationOptions)
  340. {
  341. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  342. }
  343. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  344. Action<Task<TAntecedentResult>[]> continuationAction,
  345. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
  346. TaskScheduler scheduler)
  347. {
  348. return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
  349. continuationOptions, scheduler);
  350. }
  351. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
  352. {
  353. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  354. }
  355. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  356. TaskContinuationOptions continuationOptions)
  357. {
  358. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  359. }
  360. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  361. CancellationToken cancellationToken)
  362. {
  363. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  364. }
  365. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  366. CancellationToken cancellationToken,
  367. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  368. {
  369. var t = new Task (l => {
  370. var data = (Tuple<Task[], CancellationToken>) l;
  371. Task.WaitAll (data.Item1, data.Item2);
  372. }, Tuple.Create (tasks, cancellationToken));
  373. var cont = t.ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
  374. t.Start (scheduler);
  375. return cont;
  376. }
  377. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  378. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  379. {
  380. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  381. }
  382. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  383. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  384. TaskContinuationOptions continuationOptions)
  385. {
  386. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  387. }
  388. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  389. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  390. CancellationToken cancellationToken)
  391. {
  392. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  393. }
  394. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  395. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  396. CancellationToken cancellationToken,
  397. TaskContinuationOptions continuationOptions,
  398. TaskScheduler scheduler)
  399. {
  400. return ContinueWhenAll<TResult> ((Task[]) tasks,
  401. (o) => continuationFunction (tasks),
  402. cancellationToken,
  403. continuationOptions, scheduler);
  404. }
  405. #endregion
  406. #region FromAsync IAsyncResult
  407. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
  408. {
  409. return FromAsync (asyncResult, endMethod, creationOptions);
  410. }
  411. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
  412. {
  413. return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
  414. }
  415. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  416. {
  417. if (endMethod == null)
  418. throw new ArgumentNullException ("endMethod");
  419. return TaskFactory<object>.FromIAsyncResult (asyncResult,
  420. l => {
  421. endMethod (asyncResult);
  422. return null;
  423. }, creationOptions, scheduler);
  424. }
  425. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  426. {
  427. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  428. }
  429. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
  430. {
  431. return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
  432. }
  433. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  434. {
  435. return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
  436. }
  437. #endregion
  438. #region FromAsync Begin/End Method
  439. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
  440. {
  441. return FromAsync (beginMethod, endMethod, state, creationOptions);
  442. }
  443. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  444. object state, TaskCreationOptions creationOptions)
  445. {
  446. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  447. l => { endMethod (l); return null; },
  448. state, creationOptions);
  449. }
  450. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  451. TArg1 arg1, object state)
  452. {
  453. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  454. }
  455. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  456. TArg1 arg1, 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, state, creationOptions);
  463. }
  464. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  465. Action<IAsyncResult> endMethod,
  466. TArg1 arg1, TArg2 arg2, object state)
  467. {
  468. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  469. }
  470. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  471. Action<IAsyncResult> endMethod,
  472. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  473. {
  474. if (endMethod == null)
  475. throw new ArgumentNullException ("endMethod");
  476. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  477. l => { endMethod (l); return null; },
  478. arg1, arg2, state, creationOptions);
  479. }
  480. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  481. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  482. {
  483. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  484. }
  485. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  486. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  487. {
  488. if (endMethod == null)
  489. throw new ArgumentNullException ("endMethod");
  490. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  491. l => { endMethod (l); return null; },
  492. arg1, arg2, arg3, state, creationOptions);
  493. }
  494. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  495. object state)
  496. {
  497. return FromAsync (beginMethod, endMethod, state, creationOptions);
  498. }
  499. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  500. object state, TaskCreationOptions creationOptions)
  501. {
  502. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
  503. }
  504. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  505. TArg1 arg1, object state)
  506. {
  507. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  508. }
  509. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  510. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  511. {
  512. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
  513. }
  514. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  515. Func<IAsyncResult, TResult> endMethod,
  516. TArg1 arg1, TArg2 arg2, object state)
  517. {
  518. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  519. }
  520. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  521. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  522. {
  523. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  524. }
  525. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  526. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  527. {
  528. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  529. }
  530. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  531. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  532. {
  533. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  534. }
  535. #endregion
  536. TaskScheduler GetScheduler ()
  537. {
  538. return scheduler ?? TaskScheduler.Current;
  539. }
  540. }
  541. }
  542. #endif