TaskCompletionSource.cs 3.7 KB

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