Future.cs 7.1 KB

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