TaskFactory_T.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //
  2. // TaskFactory_T.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<TResult>
  32. {
  33. TaskScheduler scheduler;
  34. TaskCreationOptions creationOptions;
  35. TaskContinuationOptions continuationOptions;
  36. CancellationToken cancellationToken;
  37. TaskFactory parent;
  38. #region ctors
  39. public TaskFactory ()
  40. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  41. {
  42. }
  43. public TaskFactory (TaskScheduler scheduler)
  44. : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
  45. {
  46. }
  47. public TaskFactory (CancellationToken cancellationToken)
  48. : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
  49. {
  50. }
  51. public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
  52. : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
  53. {
  54. }
  55. public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
  56. 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. #endregion
  66. #region StartNew for Task<TResult>
  67. public Task<TResult> StartNew (Func<TResult> function)
  68. {
  69. return StartNew (function, cancellationToken, creationOptions, scheduler);
  70. }
  71. public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
  72. {
  73. return StartNew (function, cancellationToken, creationOptions, scheduler);
  74. }
  75. public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
  76. {
  77. return StartNew (function, cancellationToken, creationOptions, scheduler);
  78. }
  79. public Task<TResult> StartNew (Func<TResult> function,
  80. CancellationToken cancellationToken,
  81. TaskCreationOptions creationOptions,
  82. TaskScheduler scheduler)
  83. {
  84. return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
  85. }
  86. public Task<TResult> StartNew (Func<object, TResult> function, object state)
  87. {
  88. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  89. }
  90. public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  91. {
  92. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  93. }
  94. public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  95. {
  96. return StartNew (function, state, cancellationToken, creationOptions, scheduler);
  97. }
  98. public Task<TResult> StartNew (Func<object, TResult> function, object state,
  99. CancellationToken cancellationToken,
  100. TaskCreationOptions creationOptions,
  101. TaskScheduler scheduler)
  102. {
  103. return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
  104. }
  105. #endregion
  106. #region Continue
  107. public Task<TResult> ContinueWhenAny (Task[] tasks,
  108. Func<Task, TResult> continuationFunction)
  109. {
  110. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  111. }
  112. public Task<TResult> ContinueWhenAny (Task[] tasks,
  113. Func<Task, TResult> continuationFunction,
  114. CancellationToken cancellationToken)
  115. {
  116. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  117. }
  118. public Task<TResult> ContinueWhenAny (Task[] tasks,
  119. Func<Task, TResult> continuationFunction,
  120. TaskContinuationOptions continuationOptions)
  121. {
  122. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  123. }
  124. public Task<TResult> ContinueWhenAny (Task[] tasks,
  125. Func<Task, TResult> continuationFunction,
  126. CancellationToken cancellationToken,
  127. TaskContinuationOptions continuationOptions,
  128. TaskScheduler scheduler)
  129. {
  130. return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  131. }
  132. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  133. Func<Task<TAntecedentResult>, TResult> continuationFunction)
  134. {
  135. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  136. }
  137. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  138. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  139. CancellationToken cancellationToken)
  140. {
  141. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  142. }
  143. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  144. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  145. TaskContinuationOptions continuationOptions)
  146. {
  147. return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  148. }
  149. public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  150. Func<Task<TAntecedentResult>, TResult> continuationFunction,
  151. CancellationToken cancellationToken,
  152. TaskContinuationOptions continuationOptions,
  153. TaskScheduler scheduler)
  154. {
  155. return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  156. }
  157. public Task<TResult> ContinueWhenAll (Task[] tasks,
  158. Func<Task[], TResult> continuationFunction)
  159. {
  160. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  161. }
  162. public Task<TResult> ContinueWhenAll (Task[] tasks,
  163. Func<Task[], TResult> continuationFunction,
  164. TaskContinuationOptions continuationOptions)
  165. {
  166. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  167. }
  168. public Task<TResult> ContinueWhenAll (Task[] tasks,
  169. Func<Task[], TResult> continuationFunction,
  170. CancellationToken cancellationToken)
  171. {
  172. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  173. }
  174. public Task<TResult> ContinueWhenAll (Task[] tasks,
  175. Func<Task[], TResult> continuationFunction,
  176. CancellationToken cancellationToken,
  177. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  178. {
  179. return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  180. }
  181. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  182. Func<Task<TAntecedentResult>[], TResult> continuationFunction)
  183. {
  184. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  185. }
  186. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  187. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  188. TaskContinuationOptions continuationOptions)
  189. {
  190. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  191. }
  192. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  193. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  194. CancellationToken cancellationToken)
  195. {
  196. return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  197. }
  198. public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
  199. Func<Task<TAntecedentResult>[], TResult> continuationFunction,
  200. CancellationToken cancellationToken,
  201. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  202. {
  203. return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
  204. }
  205. #endregion
  206. #region FromAsync
  207. const string errorMsg = "Mono's thread pool doesn't support this operation yet";
  208. [MonoLimitation(errorMsg)]
  209. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
  210. {
  211. return FromAsync (asyncResult, endMethod, creationOptions);
  212. }
  213. [MonoLimitation(errorMsg)]
  214. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  215. TaskCreationOptions creationOptions)
  216. {
  217. return FromAsync (asyncResult, endMethod, creationOptions);
  218. }
  219. [MonoLimitation(errorMsg)]
  220. public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
  221. TaskCreationOptions creationOptions, TaskScheduler scheduler)
  222. {
  223. throw new NotSupportedException (errorMsg);
  224. }
  225. [MonoLimitation(errorMsg)]
  226. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  227. Func<IAsyncResult, TResult> endMethod,
  228. object state)
  229. {
  230. throw new NotSupportedException (errorMsg);
  231. }
  232. [MonoLimitation(errorMsg)]
  233. public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
  234. Func<IAsyncResult, TResult> endMethod,
  235. object state, TaskCreationOptions creationOptions)
  236. {
  237. throw new NotSupportedException (errorMsg);
  238. }
  239. [MonoLimitation(errorMsg)]
  240. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  241. Func<IAsyncResult, TResult> endMethod,
  242. TArg1 arg1, object state)
  243. {
  244. throw new NotSupportedException (errorMsg);
  245. }
  246. [MonoLimitation(errorMsg)]
  247. public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
  248. Func<IAsyncResult, TResult> endMethod,
  249. TArg1 arg1, object state, TaskCreationOptions creationOptions)
  250. {
  251. throw new NotSupportedException (errorMsg);
  252. }
  253. [MonoLimitation(errorMsg)]
  254. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  255. Func<IAsyncResult, TResult> endMethod,
  256. TArg1 arg1, TArg2 arg2, object state)
  257. {
  258. throw new NotSupportedException (errorMsg);
  259. }
  260. [MonoLimitation(errorMsg)]
  261. public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
  262. Func<IAsyncResult, TResult> endMethod,
  263. TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
  264. {
  265. throw new NotSupportedException (errorMsg);
  266. }
  267. [MonoLimitation(errorMsg)]
  268. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  269. Func<IAsyncResult, TResult> endMethod,
  270. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
  271. {
  272. throw new NotSupportedException (errorMsg);
  273. }
  274. [MonoLimitation(errorMsg)]
  275. public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
  276. Func<IAsyncResult, TResult> endMethod,
  277. TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
  278. TaskCreationOptions creationOptions)
  279. {
  280. throw new NotSupportedException (errorMsg);
  281. }
  282. #endregion
  283. public TaskScheduler Scheduler {
  284. get {
  285. return scheduler;
  286. }
  287. }
  288. public TaskContinuationOptions ContinuationOptions {
  289. get {
  290. return continuationOptions;
  291. }
  292. }
  293. public TaskCreationOptions CreationOptions {
  294. get {
  295. return creationOptions;
  296. }
  297. }
  298. public CancellationToken CancellationToken {
  299. get {
  300. return cancellationToken;
  301. }
  302. }
  303. }
  304. }
  305. #endif