forked from parse-community/Parse-SDK-dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskFactory.cs
201 lines (174 loc) · 6.84 KB
/
TaskFactory.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System.Threading.Tasks {
public class TaskFactory {
private readonly TaskScheduler scheduler;
private readonly CancellationToken cancellationToken;
internal TaskFactory(TaskScheduler scheduler, CancellationToken cancellationToken) {
this.scheduler = scheduler;
this.cancellationToken = cancellationToken;
}
public TaskFactory(TaskScheduler scheduler)
: this(scheduler, CancellationToken.None) {
}
public TaskFactory(CancellationToken cancellationToken)
: this(TaskScheduler.FromCurrentSynchronizationContext(), cancellationToken) {
}
public TaskFactory()
: this(TaskScheduler.FromCurrentSynchronizationContext(), CancellationToken.None) {
}
public TaskFactory(CancellationToken cancellationToken,
TaskCreationOptions creationOptions,
TaskContinuationOptions continuationOptions,
TaskScheduler scheduler)
: this(scheduler, cancellationToken) {
// Just ignore the other arguments -- we don't use them.
}
public TaskScheduler Scheduler {
get {
return scheduler;
}
}
public Task<T> StartNew<T>(Func<T> func) {
var tcs = new TaskCompletionSource<T>();
scheduler.Post(() => {
try {
tcs.SetResult(func());
} catch (Exception e) {
tcs.SetException(e);
}
});
return tcs.Task;
}
public Task FromAsync<TArg1, TArg2, TArg3>(
Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
Action<IAsyncResult> endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, object state) {
return FromAsync((callback, _) => beginMethod(arg1, arg2, arg3, callback, state),
endMethod, state);
}
public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult>(
Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, object state) {
return FromAsync((callback, _) => beginMethod(arg1, arg2, arg3, callback, state),
endMethod, state);
}
public Task FromAsync<TArg1, TArg2>(
Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
Action<IAsyncResult> endMethod, TArg1 arg1, TArg2 arg2, object state) {
return FromAsync((callback, _) => beginMethod(arg1, arg2, callback, state),
endMethod, state);
}
public Task<TResult> FromAsync<TArg1, TArg2, TResult>(
Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod, TArg1 arg1, TArg2 arg2, object state) {
return FromAsync((callback, _) => beginMethod(arg1, arg2, callback, state),
endMethod, state);
}
public Task FromAsync<TArg1>(
Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
Action<IAsyncResult> endMethod, TArg1 arg1, object state) {
return FromAsync((callback, _) => beginMethod(arg1, callback, state),
endMethod, state);
}
public Task<TResult> FromAsync<TArg1, TResult>(
Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod, TArg1 arg1, object state) {
return FromAsync((callback, _) => beginMethod(arg1, callback, state),
endMethod, state);
}
public Task FromAsync(Func<AsyncCallback, object, IAsyncResult> beginMethod,
Action<IAsyncResult> endMethod,
object state) {
return FromAsync(beginMethod, result => {
endMethod(result);
return 0;
}, state);
}
public Task<TResult> FromAsync<TResult>(Func<AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, TResult> endMethod,
object state) {
var tcs = new TaskCompletionSource<TResult>();
var cancellation = cancellationToken.Register(() => tcs.TrySetCanceled());
if (cancellationToken.IsCancellationRequested) {
tcs.TrySetCanceled();
cancellation.Dispose();
return tcs.Task;
}
try {
beginMethod(result => {
try {
var value = endMethod(result);
tcs.TrySetResult(value);
cancellation.Dispose();
} catch (Exception e) {
tcs.TrySetException(e);
cancellation.Dispose();
}
}, state);
} catch (Exception e) {
tcs.TrySetException(e);
cancellation.Dispose();
}
return tcs.Task;
}
public Task ContinueWhenAll(Task[] tasks, Action<Task[]> continuationAction) {
int remaining = tasks.Length;
var tcs = new TaskCompletionSource<Task[]>();
if (remaining == 0) {
tcs.TrySetResult(tasks);
}
foreach (var task in tasks) {
task.ContinueWith(_ => {
if (Interlocked.Decrement(ref remaining) == 0) {
tcs.TrySetResult(tasks);
}
});
}
return tcs.Task.ContinueWith(t => {
continuationAction(t.Result);
});
}
}
internal class TaskFactory<T> {
private readonly TaskFactory factory;
internal TaskFactory(TaskScheduler scheduler, CancellationToken cancellationToken) {
this.factory = new TaskFactory(scheduler, cancellationToken);
}
public TaskFactory(TaskScheduler scheduler)
: this(scheduler, CancellationToken.None) {
}
public TaskFactory(CancellationToken cancellationToken)
: this(TaskScheduler.FromCurrentSynchronizationContext(), cancellationToken) {
}
public TaskFactory()
: this(TaskScheduler.FromCurrentSynchronizationContext(), CancellationToken.None) {
}
public TaskScheduler Scheduler {
get {
return factory.Scheduler;
}
}
public Task<T> FromAsync<TArg1, TArg2, TArg3>(
Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, T> endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, object state) {
return factory.FromAsync<TArg1, TArg2, TArg3, T>(beginMethod, endMethod, arg1, arg2, arg3, state);
}
public Task<T> FromAsync<TArg1, TArg2>(
Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, T> endMethod, TArg1 arg1, TArg2 arg2, object state) {
return factory.FromAsync<TArg1, TArg2, T>(beginMethod, endMethod, arg1, arg2, state);
}
public Task<T> FromAsync<TArg1>(
Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, T> endMethod, TArg1 arg1, object state) {
return factory.FromAsync<TArg1, T>(beginMethod, endMethod, arg1, state);
}
public Task<T> FromAsync(Func<AsyncCallback, object, IAsyncResult> beginMethod,
Func<IAsyncResult, T> endMethod,
object state) {
return factory.FromAsync<T>(beginMethod, endMethod, state);
}
}
}