TaskFactory.cs 30 KB

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