-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathProgressProxy.cs
179 lines (149 loc) · 4.7 KB
/
ProgressProxy.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
using System.Threading;
using System.Threading.Tasks;
namespace Signum.Utilities;
public class ProgressProxy
{
private string? currentTask;
private int min;
private int max;
private int position;
public event EventHandler<ProgressArgs>? Changed;
public CancellationToken CancellationToken;
public ProgressProxy(CancellationToken cancellationToken = default)
{
this.CancellationToken = cancellationToken;
}
public int Min
{
get { return min; }
}
public int Max
{
get { return max; }
}
public int Position
{
get { return position; }
set
{
if (min <= value && value <= max)
{
position = value;
OnChanged(ProgressAction.Position);
}
}
}
//pp?.Posittion++ does not compile, but pp?.IncrementPosition() does
public void IncrementPosition()
{
Position++;
}
public string? CurrentTask
{
get { return currentTask; }
}
public void Start(int max)
{
this.min = 0;
this.max = max;
this.position = 0;
OnChanged(ProgressAction.Interval);
}
public void Start(string currentTask)
{
this.currentTask = currentTask;
this.position = -1;
OnChanged(ProgressAction.Interval | ProgressAction.Task);
}
public void Start(int max, string currentTask)
{
Start(0, max, currentTask);
}
public void Start(int min, int max, string currentTask, int? position = null)
{
if (min < 0 || max < 0)
throw new ArgumentException("Min and Max should be greater than 0");
if (max < min)
throw new ArgumentException("Max should be greater or equal than min");
if (position.HasValue && !(min <= position && position <= max))
throw new ArgumentException("position should be between min and max");
this.currentTask = currentTask;
this.min = min;
this.position = position ?? min;
this.max = max;
OnChanged(ProgressAction.Interval | ProgressAction.Task);
}
public void NextTask(int position, string currentTask)
{
this.position = position;
this.currentTask = currentTask;
OnChanged(ProgressAction.Task | ProgressAction.Position);
}
public void NextTask(string currentTask)
{
this.position++;
this.currentTask = currentTask;
OnChanged(ProgressAction.Task | ProgressAction.Position);
}
public void Reset()
{
currentTask = null;
position = min;
OnChanged(ProgressAction.Task | ProgressAction.Position);
}
void OnChanged(ProgressAction pa)
{
this.CancellationToken.ThrowIfCancellationRequested();
Changed?.Invoke(this, new ProgressArgs(pa));
}
}
public enum ProgressAction
{
Interval = 1,
Position = 2,
Task = 4,
}
public class ProgressArgs : EventArgs
{
public readonly ProgressAction Action;
public ProgressArgs(ProgressAction a)
{
Action = a;
}
}
public static class WaitHandleExtension
{
public static async Task<bool> WaitOneAsync(this WaitHandle handle, int millisecondsTimeout, CancellationToken cancellationToken)
{
RegisteredWaitHandle? registeredHandle = null;
CancellationTokenRegistration tokenRegistration = default(CancellationTokenRegistration);
try
{
var tcs = new TaskCompletionSource<bool>();
registeredHandle = ThreadPool.RegisterWaitForSingleObject(
handle,
(state, timedOut) => ((TaskCompletionSource<bool>)state!).TrySetResult(!timedOut),
tcs,
millisecondsTimeout,
true);
tokenRegistration = cancellationToken.Register(
state => ((TaskCompletionSource<bool>)state!).TrySetCanceled(),
tcs);
return await tcs.Task;
}
finally
{
if (registeredHandle != null)
registeredHandle.Unregister(null);
tokenRegistration.Dispose();
}
}
public static Task<bool> WaitOneAsync(this WaitHandle handle, TimeSpan timeout, CancellationToken cancellationToken)
{
return handle.WaitOneAsync((int)timeout.TotalMilliseconds, cancellationToken);
}
public static Task<bool> WaitOneAsync(this WaitHandle handle, CancellationToken cancellationToken)
{
return handle.WaitOneAsync(Timeout.Infinite, cancellationToken);
}
}