Future.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #if NET_4_0
  2. // Future.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. using System;
  26. namespace System.Threading.Tasks
  27. {
  28. public class Task<TResult>: Task
  29. {
  30. TResult value;
  31. static TaskFactory<TResult> factory = new TaskFactory<TResult> ();
  32. Func<object, TResult> function;
  33. object state;
  34. public TResult Result {
  35. get {
  36. if (function != null)
  37. Wait ();
  38. return value;
  39. }
  40. internal set {
  41. this.value = value;
  42. }
  43. }
  44. public static new TaskFactory<TResult> Factory {
  45. get {
  46. return factory;
  47. }
  48. }
  49. public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
  50. {
  51. }
  52. public Task (Func<TResult> function, CancellationToken token)
  53. : this ((o) => function (), null, token, TaskCreationOptions.None)
  54. {
  55. }
  56. public Task (Func<TResult> function, TaskCreationOptions options)
  57. : this ((o) => function (), null, CancellationToken.None, options)
  58. {
  59. }
  60. public Task (Func<TResult> function, CancellationToken token, TaskCreationOptions options)
  61. : this ((o) => function (), null, token, options)
  62. {
  63. }
  64. public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
  65. {
  66. }
  67. public Task (Func<object, TResult> function, object state, CancellationToken token)
  68. : this (function, state, token, TaskCreationOptions.None)
  69. {
  70. }
  71. public Task (Func<object, TResult> function, object state, TaskCreationOptions options)
  72. : this (function, state, CancellationToken.None, options)
  73. {
  74. }
  75. public Task (Func<object, TResult> function, object state, CancellationToken token, TaskCreationOptions options)
  76. : base (null, state, token, options)
  77. {
  78. this.function = function;
  79. this.state = state;
  80. }
  81. internal override void InnerInvoke ()
  82. {
  83. if (function != null)
  84. value = function (state);
  85. function = null;
  86. state = null;
  87. }
  88. public Task ContinueWith (Action<Task<TResult>> a)
  89. {
  90. return ContinueWith (a, TaskContinuationOptions.None);
  91. }
  92. public Task ContinueWith (Action<Task<TResult>> a, TaskContinuationOptions options)
  93. {
  94. return ContinueWith (a, CancellationToken.None, options, TaskScheduler.Current);
  95. }
  96. public Task ContinueWith (Action<Task<TResult>> a, CancellationToken token)
  97. {
  98. return ContinueWith (a, token, TaskContinuationOptions.None, TaskScheduler.Current);
  99. }
  100. public Task ContinueWith (Action<Task<TResult>> a, TaskScheduler scheduler)
  101. {
  102. return ContinueWith (a, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  103. }
  104. public Task ContinueWith (Action<Task<TResult>> a, CancellationToken token,
  105. TaskContinuationOptions options, TaskScheduler scheduler)
  106. {
  107. Task t = new Task ((o) => a ((Task<TResult>)o), this, token, GetCreationOptions (options));
  108. ContinueWithCore (t, options, scheduler);
  109. return t;
  110. }
  111. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a)
  112. {
  113. return ContinueWith<TNewResult> (a, TaskContinuationOptions.None);
  114. }
  115. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, CancellationToken token)
  116. {
  117. return ContinueWith<TNewResult> (a, token, TaskContinuationOptions.None, TaskScheduler.Current);
  118. }
  119. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, TaskContinuationOptions options)
  120. {
  121. return ContinueWith<TNewResult> (a, CancellationToken.None, options, TaskScheduler.Current);
  122. }
  123. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, TaskScheduler scheduler)
  124. {
  125. return ContinueWith<TNewResult> (a, CancellationToken.None, TaskContinuationOptions.None, scheduler);
  126. }
  127. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, CancellationToken token,
  128. TaskContinuationOptions options,
  129. TaskScheduler scheduler)
  130. {
  131. Task<TNewResult> t = new Task<TNewResult> ((o) => a ((Task<TResult>)o), this, token, GetCreationOptions (options));
  132. ContinueWithCore (t, options, scheduler);
  133. return t;
  134. }
  135. }
  136. }
  137. #endif