TaskFactory_T.cs 19 KB

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