Task_T.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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}, Method = {DebuggerDisplayMethodDescription}, Result = {DebuggerDisplayResultDescription}")]
  30. [System.Diagnostics.DebuggerTypeProxy ("System.Threading.Tasks.SystemThreadingTasks_FutureDebugView`1")]
  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. 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. public static new TaskFactory<TResult> Factory {
  52. get {
  53. return factory;
  54. }
  55. }
  56. public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
  57. {
  58. }
  59. public Task (Func<TResult> function, CancellationToken cancellationToken)
  60. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, TaskCreationOptions.None)
  61. {
  62. }
  63. public Task (Func<TResult> function, TaskCreationOptions creationOptions)
  64. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, CancellationToken.None, creationOptions)
  65. {
  66. }
  67. public Task (Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
  68. : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, creationOptions)
  69. {
  70. }
  71. public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
  72. {
  73. }
  74. public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken)
  75. : this (function, state, cancellationToken, TaskCreationOptions.None)
  76. {
  77. }
  78. public Task (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
  79. : this (function, state, CancellationToken.None, creationOptions)
  80. {
  81. }
  82. public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
  83. : base (emptyAction, state, cancellationToken, creationOptions)
  84. {
  85. this.function = function;
  86. this.state = state;
  87. }
  88. internal Task (Func<object, TResult> function,
  89. object state,
  90. CancellationToken cancellationToken,
  91. TaskCreationOptions creationOptions,
  92. Task parent)
  93. : base (null, state, cancellationToken, creationOptions, parent)
  94. {
  95. this.function = function;
  96. this.state = state;
  97. }
  98. internal override void InnerInvoke ()
  99. {
  100. if (function != null)
  101. value = function (state);
  102. function = null;
  103. state = null;
  104. }
  105. public Task ContinueWith (Action<Task<TResult>> continuationAction)
  106. {
  107. return ContinueWith (continuationAction, TaskContinuationOptions.None);
  108. }
  109. public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskContinuationOptions continuationOptions)
  110. {
  111. return ContinueWith (continuationAction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  112. }
  113. public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken)
  114. {
  115. return ContinueWith (continuationAction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  116. }
  117. public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskScheduler scheduler)
  118. {
  119. return ContinueWith (continuationAction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  120. }
  121. public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken,
  122. TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
  123. {
  124. if (continuationAction == null)
  125. throw new ArgumentNullException ("continuationFunction");
  126. if (scheduler == null)
  127. throw new ArgumentNullException ("scheduler");
  128. Task t = new Task ((o) => continuationAction ((Task<TResult>)o),
  129. this,
  130. cancellationToken,
  131. GetCreationOptions (continuationOptions),
  132. this);
  133. ContinueWithCore (t, continuationOptions, scheduler);
  134. return t;
  135. }
  136. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction)
  137. {
  138. return ContinueWith<TNewResult> (continuationFunction, TaskContinuationOptions.None);
  139. }
  140. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, CancellationToken cancellationToken)
  141. {
  142. return ContinueWith<TNewResult> (continuationFunction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
  143. }
  144. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskContinuationOptions continuationOptions)
  145. {
  146. return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
  147. }
  148. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskScheduler scheduler)
  149. {
  150. return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  151. }
  152. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction,
  153. CancellationToken cancellationToken,
  154. TaskContinuationOptions continuationOptions,
  155. TaskScheduler scheduler)
  156. {
  157. Task<TNewResult> t = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>)o),
  158. this,
  159. cancellationToken,
  160. GetCreationOptions (continuationOptions),
  161. this);
  162. ContinueWithCore (t, continuationOptions, scheduler);
  163. return t;
  164. }
  165. }
  166. }
  167. #endif