Task_T.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Task_T.cs
  3. //
  4. // Copyright (c) 2008 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  25. #if NET_4_0 || MOBILE
  26. using System;
  27. namespace System.Threading.Tasks
  28. {
  29. [System.Diagnostics.DebuggerDisplay ("Id = {Id}, Status = {Status}, Result = {ResultAsString}")]
  30. [System.Diagnostics.DebuggerTypeProxy (typeof (TaskDebuggerView))]
  31. public class Task<TResult>: Task
  32. {
  33. TResult value;
  34. static TaskFactory<TResult> factory = new TaskFactory<TResult> ();
  35. static readonly Action<object> emptyAction = delegate {};
  36. internal Func<object, TResult> function;
  37. object state;
  38. [System.Diagnostics.DebuggerBrowsable (System.Diagnostics.DebuggerBrowsableState.Never)]
  39. public TResult Result {
  40. get {
  41. if (function != null)
  42. Wait ();
  43. else if (Exception != null)
  44. throw Exception;
  45. return value;
  46. }
  47. internal set {
  48. this.value = value;
  49. }
  50. }
  51. string ResultAsString {
  52. get {
  53. if ((Status & (TaskStatus.RanToCompletion)) != 0)
  54. return "" + value;
  55. return "<value not available>";
  56. }
  57. }
  58. public static new TaskFactory<TResult> Factory {
  59. get {
  60. return factory;
  61. }
  62. }
  63. public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
  64. {
  65. }
  66. public Task (Func<TResult> function, CancellationToken cancellationToken)
  67. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, TaskCreationOptions.None)
  68. {
  69. }
  70. public Task (Func<TResult> function, TaskCreationOptions creationOptions)
  71. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, CancellationToken.None, creationOptions)
  72. {
  73. }
  74. public Task (Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
  75. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, creationOptions)
  76. {
  77. }
  78. public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
  79. {
  80. }
  81. public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  82. : this (function, state, cancellationToken, TaskCreationOptions.None)
  83. {
  84. }
  85. public Task (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  86. : this (function, state, CancellationToken.None, creationOptions)
  87. {
  88. }
  89. public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
  90. : base (emptyAction, state, cancellationToken, creationOptions)
  91. {
  92. this.function = function;
  93. this.state = state;
  94. }
  95. internal Task (Func<object, TResult> function,
  96. object state,
  97. CancellationToken cancellationToken,
  98. TaskCreationOptions creationOptions,
  99. Task parent)
  100. : base (null, state, cancellationToken, creationOptions, parent)
  101. {
  102. this.function = function;
  103. this.state = state;
  104. }
  105. internal override void InnerInvoke ()
  106. {
  107. if (function != null)
  108. value = function (state);
  109. function = null;
  110. state = null;
  111. }
  112. public Task ContinueWith (Action<Task<TResult>> continuationAction)
  113. {
  114. return ContinueWith (continuationAction, TaskContinuationOptions.None);
  115. }
  116. public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskContinuationOptions continuationOptions)
  117. {
  118. return ContinueWith (continuationAction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  119. }
  120. public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken)
  121. {
  122. return ContinueWith (continuationAction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  123. }
  124. public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskScheduler scheduler)
  125. {
  126. return ContinueWith (continuationAction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  127. }
  128. public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken,
  129. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  130. {
  131. if (continuationAction == null)
  132. throw new ArgumentNullException ("continuationFunction");
  133. if (scheduler == null)
  134. throw new ArgumentNullException ("scheduler");
  135. Task t = new Task ((o) => continuationAction ((Task<TResult>)o),
  136. this,
  137. cancellationToken,
  138. GetCreationOptions (continuationOptions),
  139. this);
  140. ContinueWithCore (t, continuationOptions, scheduler);
  141. return t;
  142. }
  143. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction)
  144. {
  145. return ContinueWith<TNewResult> (continuationFunction, TaskContinuationOptions.None);
  146. }
  147. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, CancellationToken cancellationToken)
  148. {
  149. return ContinueWith<TNewResult> (continuationFunction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  150. }
  151. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskContinuationOptions continuationOptions)
  152. {
  153. return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  154. }
  155. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskScheduler scheduler)
  156. {
  157. return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  158. }
  159. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction,
  160. CancellationToken cancellationToken,
  161. TaskContinuationOptions continuationOptions,
  162. TaskScheduler scheduler)
  163. {
  164. Task<TNewResult> t = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>)o),
  165. this,
  166. cancellationToken,
  167. GetCreationOptions (continuationOptions),
  168. this);
  169. ContinueWithCore (t, continuationOptions, scheduler);
  170. return t;
  171. }
  172. }
  173. }
  174. #endif