TaskCompletionSource.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // TaskCompletionSource.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if NET_4_0 || BOOTSTRAP_NET_4_0
  27. using System;
  28. using System.Collections.Generic;
  29. namespace System.Threading.Tasks
  30. {
  31. public class TaskCompletionSource<TResult>
  32. {
  33. Task<TResult> source;
  34. public TaskCompletionSource ()
  35. {
  36. source = new Task<TResult> (null);
  37. source.SetupScheduler (TaskScheduler.Current);
  38. }
  39. public TaskCompletionSource (object state)
  40. {
  41. source = new Task<TResult> (null, state);
  42. source.SetupScheduler (TaskScheduler.Current);
  43. }
  44. public TaskCompletionSource (TaskCreationOptions options)
  45. {
  46. source = new Task<TResult> (null, options);
  47. source.SetupScheduler (TaskScheduler.Current);
  48. }
  49. public TaskCompletionSource (object state, TaskCreationOptions options)
  50. {
  51. source = new Task<TResult> (null, state, options);
  52. source.SetupScheduler (TaskScheduler.Current);
  53. }
  54. public void SetCanceled ()
  55. {
  56. if (!ApplyOperation (source.CancelReal))
  57. ThrowInvalidException ();
  58. }
  59. public void SetException (Exception e)
  60. {
  61. SetException (new Exception[] { e });
  62. }
  63. public void SetException (IEnumerable<Exception> e)
  64. {
  65. if (!ApplyOperation (() => source.HandleGenericException (new AggregateException (e))))
  66. ThrowInvalidException ();
  67. }
  68. public void SetResult (TResult result)
  69. {
  70. if (!ApplyOperation (() => source.Result = result))
  71. ThrowInvalidException ();
  72. }
  73. void ThrowInvalidException ()
  74. {
  75. throw new InvalidOperationException ("The underlying Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled.");
  76. }
  77. public bool TrySetCanceled ()
  78. {
  79. return ApplyOperation (source.CancelReal);
  80. }
  81. public bool TrySetException (Exception e)
  82. {
  83. return TrySetException (new Exception[] { e });
  84. }
  85. public bool TrySetException (IEnumerable<Exception> e)
  86. {
  87. return ApplyOperation (() => source.HandleGenericException (new AggregateException (e)));
  88. }
  89. public bool TrySetResult (TResult result)
  90. {
  91. return ApplyOperation (() => source.Result = result);
  92. }
  93. bool ApplyOperation (Action action)
  94. {
  95. if (CheckInvalidState ())
  96. return false;
  97. source.Status = TaskStatus.Running;
  98. if (action != null)
  99. action ();
  100. source.Finish ();
  101. return true;
  102. }
  103. bool CheckInvalidState ()
  104. {
  105. return source.Status == TaskStatus.RanToCompletion ||
  106. source.Status == TaskStatus.Faulted ||
  107. source.Status == TaskStatus.Canceled;
  108. }
  109. public Task<TResult> Task {
  110. get {
  111. return source;
  112. }
  113. }
  114. }
  115. }
  116. #endif