TaskFactory_T.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // TaskFactory_T.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<TResult>
  34. {
  35. readonly TaskScheduler scheduler;
  36. TaskCreationOptions creationOptions;
  37. TaskContinuationOptions continuationOptions;
  38. CancellationToken cancellationToken;
  39. TaskFactory parent;
  40. const TaskCreationOptions FromAsyncOptionsNotSupported = TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness;
  41. public TaskFactory ()
  42. : this (CancellationToken.None)
  43. {
  44. }
  45. public TaskFactory (TaskScheduler scheduler)
  46. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  47. {
  48. }
  49. public TaskFactory (CancellationToken cancellationToken)
  50. : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
  51. {
  52. }
  53. public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  54. : this (CancellationToken.None, creationOptions, continuationOptions, null)
  55. {
  56. }
  57. public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  58. {
  59. this.cancellationToken = cancellationToken;
  60. this.scheduler = scheduler;
  61. this.creationOptions = creationOptions;
  62. this.continuationOptions = continuationOptions;
  63. TaskFactory.CheckContinuationOptions (continuationOptions);
  64. this.parent = new TaskFactory (cancellationToken, creationOptions, continuationOptions, scheduler);
  65. }
  66. public TaskScheduler Scheduler {
  67. get {
  68. return scheduler;
  69. }
  70. }
  71. public TaskContinuationOptions ContinuationOptions {
  72. get {
  73. return continuationOptions;
  74. }
  75. }
  76. public TaskCreationOptions CreationOptions {
  77. get {
  78. return creationOptions;
  79. }
  80. }
  81. public CancellationToken CancellationToken {
  82. get {
  83. return cancellationToken;
  84. }
  85. }
  86. #region StartNew for Task<TResult>
  87. public Task<TResult> StartNew (Func<TResult> function)
  88. {
  89. return StartNew (function, cancellationToken, creationOptions, GetScheduler ());
  90. }
  91. public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
  92. {
  93. return StartNew (function, cancellationToken, creationOptions, GetScheduler ());
  94. }
  95. public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
  96. {
  97. return StartNew (function, cancellationToken, creationOptions, GetScheduler ());
  98. }
  99. public Task<TResult> StartNew (Func<TResult> function,
  100. CancellationToken cancellationToken,
  101. TaskCreationOptions creationOptions,
  102. TaskScheduler scheduler)
  103. {
  104. return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  105. }
  106. public Task<TResult> StartNew (Func<object, TResult> function, object state)
  107. {
  108. return StartNew (function, state, cancellationToken, creationOptions, GetScheduler ());
  109. }
  110. public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  111. {
  112. return StartNew (function, state, cancellationToken, creationOptions, GetScheduler ());
  113. }
  114. public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  115. {
  116. return StartNew (function, state, cancellationToken, creationOptions, GetScheduler ());
  117. }
  118. public Task<TResult> StartNew (Func<object, TResult> function, object state,
  119. CancellationToken cancellationToken,
  120. TaskCreationOptions creationOptions,
  121. TaskScheduler scheduler)
  122. {
  123. return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  124. }
  125. #endregion
  126. #region Continue
  127. public Task<TResult> ContinueWhenAny (Task[] tasks,
  128. Func<Task, TResult> continuationFunction)
  129. {
  130. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  131. }
  132. public Task<TResult> ContinueWhenAny (Task[] tasks,
  133. Func<Task, TResult> continuationFunction,
  134. CancellationToken cancellationToken)
  135. {
  136. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  137. }
  138. public Task<TResult> ContinueWhenAny (Task[] tasks,
  139. Func<Task, TResult> continuationFunction,
  140. TaskContinuationOptions continuationOptions)
  141. {
  142. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  143. }
  144. public Task<TResult> ContinueWhenAny (Task[] tasks,
  145. Func<Task, TResult> continuationFunction,
  146. CancellationToken cancellationToken,
  147. TaskContinuationOptions continuationOptions,
  148. TaskScheduler scheduler)
  149. {
  150. return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  151. }
  152. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  153. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  154. {
  155. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  156. }
  157. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  158. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  159. CancellationToken cancellationToken)
  160. {
  161. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  162. }
  163. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  164. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  165. TaskContinuationOptions continuationOptions)
  166. {
  167. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  168. }
  169. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  170. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  171. CancellationToken cancellationToken,
  172. TaskContinuationOptions continuationOptions,
  173. TaskScheduler scheduler)
  174. {
  175. return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  176. }
  177. public Task<TResult> ContinueWhenAll (Task[] tasks, Func<Task[], TResult> continuationFunction)
  178. {
  179. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  180. }
  181. public Task<TResult> ContinueWhenAll (Task[] tasks,
  182. Func<Task[], TResult> continuationFunction,
  183. TaskContinuationOptions continuationOptions)
  184. {
  185. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  186. }
  187. public Task<TResult> ContinueWhenAll (Task[] tasks,
  188. Func<Task[], TResult> continuationFunction,
  189. CancellationToken cancellationToken)
  190. {
  191. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  192. }
  193. public Task<TResult> ContinueWhenAll (Task[] tasks,
  194. Func<Task[], TResult> continuationFunction,
  195. CancellationToken cancellationToken,
  196. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  197. {
  198. return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  199. }
  200. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  201. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  202. {
  203. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  204. }
  205. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  206. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  207. TaskContinuationOptions continuationOptions)
  208. {
  209. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  210. }
  211. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  212. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  213. CancellationToken cancellationToken)
  214. {
  215. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
  216. }
  217. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  218. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  219. CancellationToken cancellationToken,
  220. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  221. {
  222. return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  223. }
  224. #endregion
  225. #region FromAsync
  226. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  227. {
  228. return FromAsync (asyncResult, endMethod, creationOptions);
  229. }
  230. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
  231. {
  232. return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
  233. }
  234. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  235. {
  236. return FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
  237. }
  238. internal static Task<TResult> FromIAsyncResult (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
  239. {
  240. if (asyncResult == null)
  241. throw new ArgumentNullException ("asyncResult");
  242. if (endMethod == null)
  243. throw new ArgumentNullException ("endMethod");
  244. if (scheduler == null)
  245. throw new ArgumentNullException ("scheduler");
  246. if ((creationOptions & FromAsyncOptionsNotSupported) != 0)
  247. throw new ArgumentOutOfRangeException ("creationOptions");
  248. var tcs = new TaskCompletionSource<TResult> (creationOptions);
  249. tcs.Task.function = l => endMethod (asyncResult);
  250. var task = tcs.Task;
  251. // Take quick path for completed operations
  252. if (asyncResult.IsCompleted) {
  253. try {
  254. task.RunSynchronously (scheduler);
  255. } catch (Exception e) {
  256. tcs.TrySetException (e);
  257. }
  258. } else {
  259. ThreadPool.RegisterWaitForSingleObject (asyncResult.AsyncWaitHandle,
  260. delegate {
  261. try {
  262. task.RunSynchronously (scheduler);
  263. } catch (Exception e) {
  264. tcs.TrySetException (e);
  265. }
  266. }, null, Timeout.Infinite, true);
  267. }
  268. return task;
  269. }
  270. public Task<TResult> FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  271. object state)
  272. {
  273. return FromAsync (beginMethod, endMethod, state, creationOptions);
  274. }
  275. public Task<TResult> FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  276. object state, TaskCreationOptions creationOptions)
  277. {
  278. return FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
  279. }
  280. internal static Task<TResult> FromAsyncBeginEnd (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  281. object state, TaskCreationOptions creationOptions)
  282. {
  283. if (beginMethod == null)
  284. throw new ArgumentNullException ("beginMethod");
  285. if (endMethod == null)
  286. throw new ArgumentNullException ("endMethod");
  287. if ((creationOptions & FromAsyncOptionsNotSupported) != 0)
  288. throw new ArgumentOutOfRangeException ("creationOptions");
  289. var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
  290. beginMethod (l => {
  291. try {
  292. tcs.SetResult (endMethod (l));
  293. } catch (Exception e) {
  294. tcs.SetException (e);
  295. }
  296. }, state);
  297. return tcs.Task;
  298. }
  299. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  300. TArg1 arg1, object state)
  301. {
  302. return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
  303. }
  304. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  305. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  306. {
  307. return FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
  308. }
  309. internal static Task<TResult> FromAsyncBeginEnd<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  310. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  311. {
  312. if (beginMethod == null)
  313. throw new ArgumentNullException ("beginMethod");
  314. if (endMethod == null)
  315. throw new ArgumentNullException ("endMethod");
  316. if ((creationOptions & FromAsyncOptionsNotSupported) != 0)
  317. throw new ArgumentOutOfRangeException ("creationOptions");
  318. var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
  319. beginMethod (arg1, l => {
  320. try {
  321. tcs.SetResult (endMethod (l));
  322. } catch (Exception e) {
  323. tcs.SetException (e);
  324. }
  325. }, state);
  326. return tcs.Task;
  327. }
  328. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  329. TArg1 arg1, TArg2 arg2, object state)
  330. {
  331. return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  332. }
  333. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  334. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  335. {
  336. return FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
  337. }
  338. internal static Task<TResult> FromAsyncBeginEnd<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  339. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  340. {
  341. if (beginMethod == null)
  342. throw new ArgumentNullException ("beginMethod");
  343. if (endMethod == null)
  344. throw new ArgumentNullException ("endMethod");
  345. if ((creationOptions & FromAsyncOptionsNotSupported) != 0)
  346. throw new ArgumentOutOfRangeException ("creationOptions");
  347. var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
  348. beginMethod (arg1, arg2, l => {
  349. try {
  350. tcs.SetResult (endMethod (l));
  351. } catch (Exception e) {
  352. tcs.SetException (e);
  353. }
  354. }, state);
  355. return tcs.Task;
  356. }
  357. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  358. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  359. {
  360. return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  361. }
  362. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  363. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  364. TaskCreationOptions creationOptions)
  365. {
  366. return FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
  367. }
  368. internal static Task<TResult> FromAsyncBeginEnd<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
  369. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
  370. {
  371. if (beginMethod == null)
  372. throw new ArgumentNullException ("beginMethod");
  373. if (endMethod == null)
  374. throw new ArgumentNullException ("endMethod");
  375. if ((creationOptions & FromAsyncOptionsNotSupported) != 0)
  376. throw new ArgumentOutOfRangeException ("creationOptions");
  377. var tcs = new TaskCompletionSource<TResult> (state, creationOptions);
  378. beginMethod (arg1, arg2, arg3, l => {
  379. try {
  380. tcs.SetResult (endMethod (l));
  381. } catch (Exception e) {
  382. tcs.SetException (e);
  383. }
  384. }, state);
  385. return tcs.Task;
  386. }
  387. #endregion
  388. TaskScheduler GetScheduler ()
  389. {
  390. return scheduler ?? TaskScheduler.Current;
  391. }
  392. }
  393. }
  394. #endif