TaskExt.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Task<T> Return<T>(T value, CancellationToken cancellationToken)
  10. {
  11. var tcs = new TaskCompletionSource<T>();
  12. tcs.TrySetResult(value);
  13. return tcs.Task;
  14. }
  15. public static Task<T> Throw<T>(Exception exception, CancellationToken cancellationToken)
  16. {
  17. var tcs = new TaskCompletionSource<T>();
  18. tcs.TrySetException(exception);
  19. return tcs.Task;
  20. }
  21. public static void Handle<T, R>(this Task<T> task, TaskCompletionSource<R> tcs, Action<T> success)
  22. {
  23. Handle(task, tcs, success, ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled());
  24. }
  25. public static void Handle<T, R>(this Task<T> task, TaskCompletionSource<R> tcs, Action<T> success, Action<AggregateException> error)
  26. {
  27. Handle(task, tcs, success, error, () => tcs.TrySetCanceled());
  28. }
  29. public static void Handle<T, R>(this Task<T> task, TaskCompletionSource<R> tcs, Action<T> success, Action<AggregateException> error, Action canceled)
  30. {
  31. if (task.IsFaulted)
  32. error(task.Exception);
  33. else if (task.IsCanceled)
  34. canceled();
  35. else if (task.IsCompleted)
  36. success(task.Result);
  37. }
  38. public static Task<bool> UsingEnumerator(this Task<bool> task, IDisposable disposable)
  39. {
  40. task.ContinueWith(t =>
  41. {
  42. if (t.IsFaulted)
  43. {
  44. var ignored = t.Exception; // don't remove!
  45. }
  46. if (t.IsFaulted || t.IsCanceled || !t.Result)
  47. disposable.Dispose();
  48. }, TaskContinuationOptions.ExecuteSynchronously);
  49. return task;
  50. }
  51. public static Task<bool> UsingEnumeratorSync(this Task<bool> task, IDisposable disposable)
  52. {
  53. var tcs = new TaskCompletionSource<bool>();
  54. task.ContinueWith(t =>
  55. {
  56. if (t.IsFaulted || t.IsCanceled || !t.Result)
  57. disposable.Dispose(); // TODO: Check whether we need exception handling here!
  58. t.Handle(tcs, res => tcs.TrySetResult(res));
  59. }, TaskContinuationOptions.ExecuteSynchronously);
  60. return tcs.Task;
  61. }
  62. public static Task<R> Finally<R>(this Task<R> task, Action action)
  63. {
  64. task.ContinueWith(t =>
  65. {
  66. if (t.IsFaulted)
  67. {
  68. var ignored = t.Exception; // don't remove!
  69. }
  70. action();
  71. }, TaskContinuationOptions.ExecuteSynchronously);
  72. return task;
  73. }
  74. public static Task<V> Zip<T, U, V>(this Task<T> t1, Task<U> t2, Func<T, U, V> f)
  75. {
  76. var gate = new object();
  77. var tcs = new TaskCompletionSource<V>();
  78. var i = 2;
  79. var complete = new Action<Task>(t =>
  80. {
  81. if (Interlocked.Decrement(ref i) == 0)
  82. {
  83. var exs = new List<Exception>();
  84. if (t1.IsFaulted)
  85. exs.Add(t1.Exception);
  86. if (t2.IsFaulted)
  87. exs.Add(t2.Exception);
  88. if (exs.Count > 0)
  89. tcs.TrySetException(exs);
  90. else if (t1.IsCanceled || t2.IsCanceled)
  91. tcs.TrySetCanceled();
  92. else
  93. {
  94. var res = default(V);
  95. try
  96. {
  97. res = f(t1.Result, t2.Result);
  98. }
  99. catch (Exception ex)
  100. {
  101. tcs.TrySetException(ex);
  102. return;
  103. }
  104. tcs.TrySetResult(res);
  105. }
  106. }
  107. });
  108. t1.ContinueWith(complete);
  109. t2.ContinueWith(complete);
  110. return tcs.Task;
  111. }
  112. }
  113. }