Future.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 new Exception Exception {
  45. get {
  46. return base.Exception;
  47. }
  48. internal set {
  49. base.Exception = value;
  50. }
  51. }
  52. public static TaskFactory<TResult> Factory {
  53. get {
  54. return factory;
  55. }
  56. }
  57. public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
  58. {
  59. }
  60. public Task (Func<TResult> function, TaskCreationOptions options) : this ((o) => function (), null, options)
  61. {
  62. }
  63. public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
  64. {
  65. }
  66. public Task (Func<object, TResult> function, object state, TaskCreationOptions options)
  67. : base (null, state, options)
  68. {
  69. this.function = function;
  70. this.state = state;
  71. }
  72. internal override void InnerInvoke ()
  73. {
  74. if (function != null)
  75. value = function (state);
  76. function = null;
  77. state = null;
  78. }
  79. public Task ContinueWith (Action<Task<TResult>> a)
  80. {
  81. return ContinueWith (a, TaskContinuationOptions.None);
  82. }
  83. public Task ContinueWith (Action<Task<TResult>> a, TaskContinuationOptions options)
  84. {
  85. return ContinueWith (a, options, TaskScheduler.Current);
  86. }
  87. public Task ContinueWith (Action<Task<TResult>> a, TaskScheduler scheduler)
  88. {
  89. return ContinueWith (a, TaskContinuationOptions.None, scheduler);
  90. }
  91. public Task ContinueWith (Action<Task<TResult>> a, TaskContinuationOptions options, TaskScheduler scheduler)
  92. {
  93. Task t = new Task ((o) => a ((Task<TResult>)o), this, TaskCreationOptions.None);
  94. ContinueWithCore (t, options, scheduler);
  95. return t;
  96. }
  97. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a)
  98. {
  99. return ContinueWith<TNewResult> (a, TaskContinuationOptions.None);
  100. }
  101. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, TaskContinuationOptions options)
  102. {
  103. return ContinueWith<TNewResult> (a, options, TaskScheduler.Current);
  104. }
  105. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, TaskScheduler scheduler)
  106. {
  107. return ContinueWith<TNewResult> (a, TaskContinuationOptions.None, scheduler);
  108. }
  109. public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, TaskContinuationOptions options,
  110. TaskScheduler scheduler)
  111. {
  112. Task<TNewResult> t = new Task<TNewResult> ((o) => a ((Task<TResult>)o), this, TaskCreationOptions.None);
  113. ContinueWithCore (t, options, scheduler);
  114. return t;
  115. }
  116. }
  117. }
  118. #endif