1
0

TaskExt.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace System.Threading.Tasks
  6. {
  7. static class TaskExt
  8. {
  9. public static readonly Task<bool> True;
  10. public static readonly Task<bool> False;
  11. static TaskExt()
  12. {
  13. True = Return(true);
  14. False = Return(false);
  15. }
  16. public static Task<T> Return<T>(T value)
  17. {
  18. var tcs = new TaskCompletionSource<T>();
  19. tcs.TrySetResult(value);
  20. return tcs.Task;
  21. }
  22. public static Task<T> Throw<T>(Exception exception)
  23. {
  24. var tcs = new TaskCompletionSource<T>();
  25. tcs.TrySetException(exception);
  26. return tcs.Task;
  27. }
  28. public static void Handle<T, R>(this Task<T> task, TaskCompletionSource<R> tcs, Action<T> success)
  29. {
  30. if (task.IsFaulted)
  31. tcs.TrySetException(task.Exception);
  32. else if (task.IsCanceled)
  33. tcs.TrySetCanceled();
  34. else if (task.IsCompleted)
  35. success(task.Result);
  36. }
  37. public static void Handle<T, R>(this Task<T> task, TaskCompletionSource<R> tcs, Action<T> success, Action<AggregateException> error)
  38. {
  39. if (task.IsFaulted)
  40. error(task.Exception);
  41. else if (task.IsCanceled)
  42. tcs.TrySetCanceled();
  43. else if (task.IsCompleted)
  44. success(task.Result);
  45. }
  46. public static void Handle<T, R>(this Task<T> task, TaskCompletionSource<R> tcs, Action<T> success, Action<AggregateException> error, Action canceled)
  47. {
  48. if (task.IsFaulted)
  49. error(task.Exception);
  50. else if (task.IsCanceled)
  51. canceled();
  52. else if (task.IsCompleted)
  53. success(task.Result);
  54. }
  55. public static Task<bool> UsingEnumerator(this Task<bool> task, IDisposable disposable)
  56. {
  57. task.ContinueWith(t =>
  58. {
  59. if (t.IsFaulted)
  60. {
  61. var ignored = t.Exception; // don't remove!
  62. }
  63. if (t.IsFaulted || t.IsCanceled || !t.Result)
  64. disposable.Dispose();
  65. }, TaskContinuationOptions.ExecuteSynchronously);
  66. return task;
  67. }
  68. public static Task Then<T>(this Task<T> task, Action<Task<T>> continuation)
  69. {
  70. //
  71. // Central location to deal with continuations; allows for experimentation with flags.
  72. // Note that right now, we don't go for synchronous execution. Users can block on the
  73. // task returned from MoveNext, which can cause deadlocks (e.g. typical uses of GroupBy
  74. // involve some aggregate). We'd need deeper asynchrony to make this work with less
  75. // spawning of tasks.
  76. //
  77. return task.ContinueWith(continuation);
  78. }
  79. public static Task<R> Then<T, R>(this Task<T> task, Func<Task<T>, R> continuation)
  80. {
  81. //
  82. // See comment on Then<T> for rationale.
  83. //
  84. return task.ContinueWith(continuation);
  85. }
  86. public static Task<bool> UsingEnumeratorSync(this Task<bool> task, IDisposable disposable)
  87. {
  88. var tcs = new TaskCompletionSource<bool>();
  89. task.ContinueWith(t =>
  90. {
  91. if (t.IsFaulted || t.IsCanceled || !t.Result)
  92. disposable.Dispose(); // TODO: Check whether we need exception handling here!
  93. t.Handle(tcs, res => tcs.TrySetResult(res));
  94. }, TaskContinuationOptions.ExecuteSynchronously);
  95. return tcs.Task;
  96. }
  97. public static Task<R> Finally<R>(this Task<R> task, Action action)
  98. {
  99. task.ContinueWith(t =>
  100. {
  101. if (t.IsFaulted)
  102. {
  103. var ignored = t.Exception; // don't remove!
  104. }
  105. action();
  106. }, TaskContinuationOptions.ExecuteSynchronously);
  107. return task;
  108. }
  109. public static Task<V> Zip<T, U, V>(this Task<T> t1, Task<U> t2, Func<T, U, V> f)
  110. {
  111. var gate = new object();
  112. var tcs = new TaskCompletionSource<V>();
  113. var i = 2;
  114. var complete = new Action<Task>(t =>
  115. {
  116. if (Interlocked.Decrement(ref i) == 0)
  117. {
  118. var exs = new List<Exception>();
  119. if (t1.IsFaulted)
  120. exs.Add(t1.Exception);
  121. if (t2.IsFaulted)
  122. exs.Add(t2.Exception);
  123. if (exs.Count > 0)
  124. tcs.TrySetException(exs);
  125. else if (t1.IsCanceled || t2.IsCanceled)
  126. tcs.TrySetCanceled();
  127. else
  128. {
  129. var res = default(V);
  130. try
  131. {
  132. res = f(t1.Result, t2.Result);
  133. }
  134. catch (Exception ex)
  135. {
  136. tcs.TrySetException(ex);
  137. return;
  138. }
  139. tcs.TrySetResult(res);
  140. }
  141. }
  142. });
  143. t1.ContinueWith(complete);
  144. t2.ContinueWith(complete);
  145. return tcs.Task;
  146. }
  147. }
  148. }