Future.cs 7.3 KB

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