TaskFactory.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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
  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. CheckContinueArguments (tasks, continuationAction, continuationOptions, scheduler);
  201. var cont = Task.WhenAnyCore (tasks).ContinueWith (TaskActionInvoker.CreateSelected (continuationAction), cancellationToken, continuationOptions, scheduler);
  202. return cont;
  203. }
  204. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  205. Action<Task<TAntecedentResult>> continuationAction)
  206. {
  207. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  208. }
  209. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  210. Action<Task<TAntecedentResult>> continuationAction,
  211. CancellationToken cancellationToken)
  212. {
  213. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  214. }
  215. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  216. Action<Task<TAntecedentResult>> continuationAction,
  217. TaskContinuationOptions continuationOptions)
  218. {
  219. return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  220. }
  221. public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  222. Action<Task<TAntecedentResult>> continuationAction,
  223. CancellationToken cancellationToken,
  224. TaskContinuationOptions continuationOptions,
  225. TaskScheduler scheduler)
  226. {
  227. return ContinueWhenAny ((Task[]) tasks,
  228. (o) => continuationAction ((Task<TAntecedentResult>)o),
  229. cancellationToken, continuationOptions, scheduler);
  230. }
  231. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
  232. {
  233. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  234. }
  235. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  236. Func<Task, TResult> continuationFunction,
  237. CancellationToken cancellationToken)
  238. {
  239. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  240. }
  241. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  242. Func<Task, TResult> continuationFunction,
  243. TaskContinuationOptions continuationOptions)
  244. {
  245. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  246. }
  247. public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
  248. Func<Task, TResult> continuationFunction,
  249. CancellationToken cancellationToken,
  250. TaskContinuationOptions continuationOptions,
  251. TaskScheduler scheduler)
  252. {
  253. CheckContinueArguments (tasks, continuationFunction, continuationOptions, scheduler);
  254. var cont = Task.WhenAnyCore (tasks).ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
  255. return cont;
  256. }
  257. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  258. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  259. {
  260. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  261. }
  262. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  263. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  264. CancellationToken cancellationToken)
  265. {
  266. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  267. }
  268. public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  269. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  270. TaskContinuationOptions continuationOptions)
  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. TaskContinuationOptions continuationOptions,
  278. TaskScheduler scheduler)
  279. {
  280. return ContinueWhenAny<TResult> ((Task[])tasks,
  281. (t) => continuationFunction((Task<TAntecedentResult>)t),
  282. cancellationToken,
  283. continuationOptions,
  284. scheduler);
  285. }
  286. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
  287. {
  288. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  289. }
  290. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
  291. {
  292. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  293. }
  294. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
  295. TaskContinuationOptions continuationOptions)
  296. {
  297. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  298. }
  299. public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
  300. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  301. {
  302. CheckContinueArguments (tasks, continuationAction, continuationOptions, scheduler);
  303. var cont = Task.WhenAllCore (tasks).ContinueWith (TaskActionInvoker.Create (continuationAction, tasks), cancellationToken, continuationOptions, scheduler);
  304. return cont;
  305. }
  306. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  307. Action<Task<TAntecedentResult>[]> continuationAction)
  308. {
  309. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  310. }
  311. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  312. Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
  313. {
  314. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  315. }
  316. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
  317. TaskContinuationOptions continuationOptions)
  318. {
  319. return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
  320. }
  321. public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  322. Action<Task<TAntecedentResult>[]> continuationAction,
  323. CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
  324. TaskScheduler scheduler)
  325. {
  326. return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
  327. continuationOptions, scheduler);
  328. }
  329. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
  330. {
  331. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  332. }
  333. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  334. TaskContinuationOptions continuationOptions)
  335. {
  336. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  337. }
  338. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  339. CancellationToken cancellationToken)
  340. {
  341. return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  342. }
  343. public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
  344. CancellationToken cancellationToken,
  345. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  346. {
  347. CheckContinueArguments (tasks, continuationFunction, continuationOptions, scheduler);
  348. var cont = Task.WhenAllCore (tasks).ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
  349. return cont;
  350. }
  351. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  352. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  353. {
  354. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  355. }
  356. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  357. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  358. TaskContinuationOptions continuationOptions)
  359. {
  360. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  361. }
  362. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  363. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  364. CancellationToken cancellationToken)
  365. {
  366. return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  367. }
  368. public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
  369. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  370. CancellationToken cancellationToken,
  371. TaskContinuationOptions continuationOptions,
  372. TaskScheduler scheduler)
  373. {
  374. return ContinueWhenAll<TResult> ((Task[]) tasks,
  375. (o) => continuationFunction (tasks),
  376. cancellationToken,
  377. continuationOptions, scheduler);
  378. }
  379. #endregion
  380. #region FromAsync IAsyncResult
  381. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
  382. {
  383. return FromAsync (asyncResult, endMethod, creationOptions);
  384. }
  385. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
  386. {
  387. return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
  388. }
  389. public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  390. {
  391. if (endMethod == null)
  392. throw new ArgumentNullException ("endMethod");
  393. return TaskFactory<object>.FromIAsyncResult (asyncResult,
  394. l => {
  395. endMethod (asyncResult);
  396. return null;
  397. }, creationOptions, scheduler);
  398. }
  399. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  400. {
  401. return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
  402. }
  403. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
  404. {
  405. return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
  406. }
  407. public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  408. {
  409. return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
  410. }
  411. #endregion
  412. #region FromAsync Begin/End Method
  413. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
  414. {
  415. return FromAsync (beginMethod, endMethod, state, creationOptions);
  416. }
  417. public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  418. object state, TaskCreationOptions creationOptions)
  419. {
  420. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  421. l => { endMethod (l); return null; },
  422. state, creationOptions);
  423. }
  424. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  425. TArg1 arg1, object state)
  426. {
  427. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  428. }
  429. public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  430. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  431. {
  432. if (endMethod == null)
  433. throw new ArgumentNullException ("endMethod");
  434. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  435. l => { endMethod (l); return null; },
  436. arg1, 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)
  441. {
  442. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  443. }
  444. public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  445. Action<IAsyncResult> endMethod,
  446. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  447. {
  448. if (endMethod == null)
  449. throw new ArgumentNullException ("endMethod");
  450. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  451. l => { endMethod (l); return null; },
  452. arg1, arg2, state, creationOptions);
  453. }
  454. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  455. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  456. {
  457. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  458. }
  459. public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
  460. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  461. {
  462. if (endMethod == null)
  463. throw new ArgumentNullException ("endMethod");
  464. return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
  465. l => { endMethod (l); return null; },
  466. arg1, arg2, arg3, state, creationOptions);
  467. }
  468. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  469. object state)
  470. {
  471. return FromAsync (beginMethod, endMethod, state, creationOptions);
  472. }
  473. public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  474. object state, TaskCreationOptions creationOptions)
  475. {
  476. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
  477. }
  478. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  479. TArg1 arg1, object state)
  480. {
  481. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  482. }
  483. public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  484. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  485. {
  486. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
  487. }
  488. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
  489. Func<IAsyncResult, TResult> endMethod,
  490. TArg1 arg1, TArg2 arg2, object state)
  491. {
  492. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  493. }
  494. public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  495. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  496. {
  497. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  498. }
  499. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  500. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  501. {
  502. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  503. }
  504. public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  505. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  506. {
  507. return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  508. }
  509. #endregion
  510. TaskScheduler GetScheduler ()
  511. {
  512. return scheduler ?? TaskScheduler.Current;
  513. }
  514. static void CheckContinueArguments (Task[] tasks, object continuationAction, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  515. {
  516. if (tasks == null)
  517. throw new ArgumentNullException ("tasks");
  518. if (tasks.Length == 0)
  519. throw new ArgumentException ("The tasks argument contains no tasks", "tasks");
  520. foreach (var ta in tasks) {
  521. if (ta == null)
  522. throw new ArgumentException ("The tasks argument contains a null value", "tasks");
  523. }
  524. if (continuationAction == null)
  525. throw new ArgumentNullException ("continuationAction");
  526. if (scheduler == null)
  527. throw new ArgumentNullException ("scheduler");
  528. const TaskContinuationOptions notAllowedOptions =
  529. TaskContinuationOptions.NotOnRanToCompletion |
  530. TaskContinuationOptions.NotOnFaulted |
  531. TaskContinuationOptions.NotOnCanceled |
  532. TaskContinuationOptions.OnlyOnRanToCompletion |
  533. TaskContinuationOptions.OnlyOnFaulted |
  534. TaskContinuationOptions.OnlyOnCanceled;
  535. if ((continuationOptions & notAllowedOptions) != 0)
  536. throw new ArgumentOutOfRangeException ("continuationOptions");
  537. }
  538. }
  539. }
  540. #endif