-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskExtensions.cs
173 lines (155 loc) · 6 KB
/
TaskExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Diagnostics.Contracts;
using System.Threading.Tasks;
namespace Open.Threading.Tasks;
public static class TaskExtensions
{
/// <summary>
/// Returns true if the target Task has not yet run, is waiting, or is running, else returns false.
/// </summary>
public static bool IsActive(this Task target)
{
if (target is null) throw new ArgumentNullException(nameof(target));
Contract.EndContractBlock();
return target.Status switch
{
TaskStatus.Created or
TaskStatus.Running or
TaskStatus.WaitingForActivation or
TaskStatus.WaitingForChildrenToComplete or
TaskStatus.WaitingToRun => true,
_ => false,
};
}
/// <summary>
/// Checks the status of the task and attempts to start it if waiting to start (TaskStatus.Created).
/// </summary>
/// <param name="target">The task to ensure start.</param>
/// <param name="scheduler">Optional scheduler to use.</param>
/// <returns>True if start attempt was successful.</returns>
public static bool EnsureStarted(this Task target, TaskScheduler? scheduler = default)
{
if (target is null) throw new ArgumentNullException(nameof(target));
if (target.Status != TaskStatus.Created) return false;
try
{
if (scheduler is null)
target.Start();
else
target.Start(scheduler);
return true;
}
catch (InvalidOperationException)
{
// Even though we've checked the status, it's possible it could have been started. We can't guarantee proper handling without a trap here.
#pragma warning disable RCS1236 // Use exception filter.
if (target.Status == TaskStatus.Created)
throw; // Something wierd must have happened if we arrived here.
#pragma warning restore RCS1236 // Use exception filter.
}
return false;
}
/// <summary>
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if completed and not cancelled.
/// </summary>
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
/// <param name="target">The task.</param>
/// <param name="action">The action to perform if fullfulled.</param>
/// <returns>The target object. Allows for method chaining.</returns>
public static TTask OnFullfilled<TTask>(this TTask target, Action action)
where TTask : Task
{
if (target is null) throw new ArgumentNullException(nameof(target));
target.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion) action();
});
return target;
}
/// <summary>
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if completed and not cancelled.
/// </summary>
/// <typeparam name="T">The return type is the same as the target.</typeparam>
/// <param name="target">The task.</param>
/// <param name="action">The action to perform if fullfulled.</param>
/// <returns>The target object. Allows for method chaining.</returns>
public static Task<T> OnFullfilled<T>(this Task<T> target, Action<T> action)
{
if (target is null) throw new ArgumentNullException(nameof(target));
target.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion) action(task.Result);
});
return target;
}
/// <summary>
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if completed and not cancelled.
/// </summary>
/// <typeparam name="TTask">The task type.</typeparam>
/// <typeparam name="T">The return type of the task.</typeparam>
/// <param name="target">The task.</param>
/// <param name="action">The action to perform if fullfulled.</param>
/// <returns>The target object. Allows for method chaining.</returns>
public static TTask OnFullfilled<TTask, T>(this TTask target, Func<T> action)
where TTask : Task
{
if (target is null) throw new ArgumentNullException(nameof(target));
target.ContinueWith(task =>
{
if (task.Status == TaskStatus.RanToCompletion) action();
});
return target;
}
/// <summary>
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if faulted.
/// </summary>
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
/// <param name="target">The task.</param>
/// <param name="action">The action to perform if faulted.</param>
/// <returns>The target object. Allows for method chaining.</returns>
public static TTask OnFaulted<TTask>(this TTask target, Action<Exception> action)
where TTask : Task
{
if (target is null) throw new ArgumentNullException(nameof(target));
target.ContinueWith(task =>
{
if (task.IsFaulted) action(task.Exception);
});
return target;
}
/// <summary>
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if cancelled.
/// </summary>
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
/// <param name="target">The task.</param>
/// <param name="action">The action to perform if cancelled.</param>
/// <returns>The target object. Allows for method chaining.</returns>
public static TTask OnCancelled<TTask>(this TTask target, Action action)
where TTask : Task
{
if (target is null) throw new ArgumentNullException(nameof(target));
target.ContinueWith(task =>
{
if (!task.IsCanceled) action();
});
return target;
}
/// <summary>
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if cancelled.
/// </summary>
/// <typeparam name="TTask">The task type.</typeparam>
/// <typeparam name="T">The return type of the task.</typeparam>
/// <param name="target">The task.</param>
/// <param name="action">The action to perform if cancelled.</param>
/// <returns>The target object. Allows for method chaining.</returns>
public static TTask OnCancelled<TTask, T>(this TTask target, Func<T> action)
where TTask : Task
{
if (target is null) throw new ArgumentNullException(nameof(target));
target.ContinueWith(task =>
{
if (!task.IsCanceled) action();
});
return target;
}
}