Task_T.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // Task_T.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (c) 2008 Jérémie "Garuma" Laval
  8. // Copyright 2011 Xamarin Inc.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. //
  28. //
  29. #if NET_4_0 || MOBILE
  30. using System;
  31. using System.Runtime.CompilerServices;
  32. namespace System.Threading.Tasks
  33. {
  34. [System.Diagnostics.DebuggerDisplay ("Id = {Id}, Status = {Status}, Result = {ResultAsString}")]
  35. [System.Diagnostics.DebuggerTypeProxy (typeof (TaskDebuggerView))]
  36. public class Task<TResult> : Task
  37. {
  38. internal static new readonly Task<TResult> Canceled = new Task<TResult> (TaskStatus.Canceled);
  39. static readonly TaskFactory<TResult> factory = new TaskFactory<TResult> ();
  40. TResult value;
  41. internal Func<object, TResult> function;
  42. object state;
  43. [System.Diagnostics.DebuggerBrowsable (System.Diagnostics.DebuggerBrowsableState.Never)]
  44. public TResult Result {
  45. get {
  46. if (function != null)
  47. Wait ();
  48. else if (Exception != null)
  49. throw Exception;
  50. return value;
  51. }
  52. internal set {
  53. this.value = value;
  54. }
  55. }
  56. string ResultAsString {
  57. get {
  58. if ((Status & (TaskStatus.RanToCompletion)) != 0)
  59. return "" + value;
  60. return "<value not available>";
  61. }
  62. }
  63. public static new TaskFactory<TResult> Factory {
  64. get {
  65. return factory;
  66. }
  67. }
  68. public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
  69. {
  70. }
  71. public Task (Func<TResult> function, CancellationToken cancellationToken)
  72. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, TaskCreationOptions.None)
  73. {
  74. }
  75. public Task (Func<TResult> function, TaskCreationOptions creationOptions)
  76. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, CancellationToken.None, creationOptions)
  77. {
  78. }
  79. public Task (Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
  80. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, creationOptions)
  81. {
  82. }
  83. public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
  84. {
  85. }
  86. public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  87. : this (function, state, cancellationToken, TaskCreationOptions.None)
  88. {
  89. }
  90. public Task (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  91. : this (function, state, CancellationToken.None, creationOptions)
  92. {
  93. }
  94. public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
  95. : base (delegate { }, state, cancellationToken, creationOptions)
  96. {
  97. if (function == null)
  98. throw new ArgumentNullException ("function");
  99. this.function = function;
  100. this.state = state;
  101. }
  102. internal Task (Func<object, TResult> function,
  103. object state,
  104. CancellationToken cancellationToken,
  105. TaskCreationOptions creationOptions,
  106. Task parent)
  107. : base (emptyAction, state, cancellationToken, creationOptions, parent)
  108. {
  109. this.function = function;
  110. this.state = state;
  111. }
  112. internal Task (TaskStatus status)
  113. : base (status)
  114. {
  115. }
  116. internal override void InnerInvoke ()
  117. {
  118. if (function != null)
  119. value = function (state);
  120. function = null;
  121. state = null;
  122. }
  123. public Task ContinueWith (Action<Task<TResult>> continuationAction)
  124. {
  125. return ContinueWith (continuationAction, TaskContinuationOptions.None);
  126. }
  127. public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskContinuationOptions continuationOptions)
  128. {
  129. return ContinueWith (continuationAction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  130. }
  131. public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken)
  132. {
  133. return ContinueWith (continuationAction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  134. }
  135. public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskScheduler scheduler)
  136. {
  137. return ContinueWith (continuationAction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  138. }
  139. public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken,
  140. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  141. {
  142. if (continuationAction == null)
  143. throw new ArgumentNullException ("continuationAction");
  144. if (scheduler == null)
  145. throw new ArgumentNullException ("scheduler");
  146. Task t = new Task (l => continuationAction ((Task<TResult>)l),
  147. this,
  148. cancellationToken,
  149. GetCreationOptions (continuationOptions),
  150. this);
  151. ContinueWithCore (t, continuationOptions, scheduler);
  152. return t;
  153. }
  154. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction)
  155. {
  156. return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Current);
  157. }
  158. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, CancellationToken cancellationToken)
  159. {
  160. return ContinueWith<TNewResult> (continuationFunction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  161. }
  162. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskContinuationOptions continuationOptions)
  163. {
  164. return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  165. }
  166. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskScheduler scheduler)
  167. {
  168. return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  169. }
  170. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction,
  171. CancellationToken cancellationToken,
  172. TaskContinuationOptions continuationOptions,
  173. TaskScheduler scheduler)
  174. {
  175. if (continuationFunction == null)
  176. throw new ArgumentNullException ("continuationFunction");
  177. if (scheduler == null)
  178. throw new ArgumentNullException ("scheduler");
  179. Task<TNewResult> t = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>)o),
  180. this,
  181. cancellationToken,
  182. GetCreationOptions (continuationOptions),
  183. this);
  184. ContinueWithCore (t, continuationOptions, scheduler);
  185. return t;
  186. }
  187. #if NET_4_5
  188. public Task ContinueWith (Action<Task<TResult>, object> continuationAction, object state)
  189. {
  190. return ContinueWith (continuationAction, state, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Current);
  191. }
  192. public Task ContinueWith (Action<Task<TResult>, object> continuationAction, object state, CancellationToken cancellationToken)
  193. {
  194. return ContinueWith (continuationAction, state, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  195. }
  196. public Task ContinueWith (Action<Task<TResult>, object> continuationAction, object state, TaskContinuationOptions continuationOptions)
  197. {
  198. return ContinueWith (continuationAction, state, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  199. }
  200. public Task ContinueWith (Action<Task<TResult>, object> continuationAction, object state, TaskScheduler scheduler)
  201. {
  202. return ContinueWith (continuationAction, state, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  203. }
  204. public Task ContinueWith (Action<Task<TResult>, object> continuationAction, object state, CancellationToken cancellationToken,
  205. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  206. {
  207. if (continuationAction == null)
  208. throw new ArgumentNullException ("continuationAction");
  209. if (scheduler == null)
  210. throw new ArgumentNullException ("scheduler");
  211. var t = new Task (l => continuationAction (this, l),
  212. state,
  213. cancellationToken,
  214. GetCreationOptions (continuationOptions),
  215. this);
  216. ContinueWithCore (t, continuationOptions, scheduler);
  217. return t;
  218. }
  219. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, object, TNewResult> continuationFunction, object state)
  220. {
  221. return ContinueWith<TNewResult> (continuationFunction, state, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Current);
  222. }
  223. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, object, TNewResult> continuationFunction, object state, CancellationToken cancellationToken)
  224. {
  225. return ContinueWith<TNewResult> (continuationFunction, state, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  226. }
  227. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, object, TNewResult> continuationFunction, object state, TaskContinuationOptions continuationOptions)
  228. {
  229. return ContinueWith<TNewResult> (continuationFunction, state, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  230. }
  231. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, object, TNewResult> continuationFunction, object state, TaskScheduler scheduler)
  232. {
  233. return ContinueWith<TNewResult> (continuationFunction, state, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  234. }
  235. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, object, TNewResult> continuationFunction, object state,
  236. CancellationToken cancellationToken,
  237. TaskContinuationOptions continuationOptions,
  238. TaskScheduler scheduler)
  239. {
  240. if (continuationFunction == null)
  241. throw new ArgumentNullException ("continuationFunction");
  242. if (scheduler == null)
  243. throw new ArgumentNullException ("scheduler");
  244. var t = new Task<TNewResult> (l => continuationFunction (this, l),
  245. state,
  246. cancellationToken,
  247. GetCreationOptions (continuationOptions),
  248. this);
  249. ContinueWithCore (t, continuationOptions, scheduler);
  250. return t;
  251. }
  252. public new ConfiguredTaskAwaitable<TResult> ConfigureAwait (bool continueOnCapturedContext)
  253. {
  254. return new ConfiguredTaskAwaitable<TResult> (this, continueOnCapturedContext);
  255. }
  256. public new TaskAwaiter<TResult> GetAwaiter ()
  257. {
  258. return new TaskAwaiter<TResult> (this);
  259. }
  260. #endif
  261. }
  262. }
  263. #endif