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