-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobManager.cs
307 lines (291 loc) · 7.94 KB
/
JobManager.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace ezThread
{
public class JobManager
{
private List<EZTHREAD> ezthreads = new List<EZTHREAD>();
private Queue<Job> qjob = new Queue<Job>();
private bool started = false;
public JobManager(List<Job> Jobs, int threadstouse)
{
qjob = new Queue<Job>(Jobs);
for (int i = 0; i < threadstouse; i++)
{
addThread();
}
}
public JobManager(int threadstouse)
{
for (int i = 0; i < threadstouse; i++)
{
addThread();
}
}
//Requires the cancellation token source of the token you gave to your threads
public JobManager(List<EZTHREAD> threads)
{
ezthreads = threads;
}
public JobManager()
{
}
//Starts the threads
public void startThreads()
{
if (ezthreads.Count <= 0)
{
throw new Exception("Thread list empty");
}
else if (qjob.Count <= 0)
{
throw new Exception("Job list empty");
}
else
{
foreach (var thread in ezthreads)
{
thread.start();
}
started = true;
}
}
//Pauses all threads
public void pauseThreads()
{
foreach (var ezthread in ezthreads)
{
ezthread.pause();
}
started = false;
}
//Resumes all threads
public void resumeThreads()
{
foreach (var ezthread in ezthreads)
{
ezthread.resume();
}
started = true;
}
//Returns true if all threads are paused
public bool isPaused()
{
foreach (var ezthread in ezthreads)
{
if (!ezthread.isPaused())
{
return false;
}
}
return true;
}
//Kills all the threads
public void killThreads()
{
foreach (var ezthread in ezthreads)
{
ezthread.killThread();
}
started = false;
}
//Removes job to list
public void removeJob(Job j)
{
qjob = new Queue<Job>(qjob.Where(s => s != j));
}
//Adds job to list
public void addJob(Job j)
{
qjob.Enqueue(j);
}
//Kills specific thread (Thread IDs start at 0 and ends at threadAmount-1)
public void killThread(int id)
{
ezthreads[id].killThread();
}
//Returns true if all threads are done/off
public bool isDone()
{
foreach (var ezthread in ezthreads)
{
if (ezthread.t.IsAlive)
{
return false;
}
}
started = false;
return true;
}
//Adds thread to thread list and starts them if the manager is running
public void addThread(int Amount = 1)
{
for (int i = 0; i < Amount; i++)
{
EZTHREAD ezt = new EZTHREAD(qjob, ezthreads.Count);
ezthreads.Add(ezt);
if (started)
{
ezt.start();
}
}
}
//Deletes specified amount of threads (Preferably use it when the manager isn't running)
public void decreaseThreads(int amount)
{
if (amount < ezthreads.Count)
{
for (int i = 0; i < amount; i++)
{
ezthreads.Last().killThread();
ezthreads.Remove(ezthreads.Last());
}
}
else
{
foreach (EZTHREAD ezt in ezthreads)
{
ezt.killThread();
}
ezthreads.Clear();
started = false;
}
}
//Deletes specified percentage of threads (Preferably use it when the manager isn't running)
public void decreaseThreads(double percentage)
{
int amount = Convert.ToInt32(ezthreads.Count * (percentage / 100));
if (amount < ezthreads.Count)
{
for (int i = 0; i < amount; ++i)
{
ezthreads.Last().killThread();
ezthreads.Remove(ezthreads.Last());
}
}
else
{
foreach (EZTHREAD ezt in ezthreads)
{
ezt.killThread();
}
ezthreads.Clear();
started = false;
}
}
//Returns the amount of threads in the manager
public int threadAmount()
{
return ezthreads.Count;
}
}
public class EZTHREAD
{
public Thread t;
public readonly int ID;
private Queue<Job> jobs;
private bool paused = false;
private bool killthread = false;
public EZTHREAD(Queue<Job> lj, int id)
{
jobs = lj;
ID = id;
ThreadStart ts = new ThreadStart(() =>
{
while (true)
{
if (paused)
{
try
{
Thread.Sleep(Timeout.Infinite);
}
catch
{
}
}
else if (killthread)
{
break;
}
else if (jobs.Count != 0)
{
var j = jobs.Dequeue();
if (j != null)
{
j.execute();
}
}
else
{
break;
}
}
});
t = new Thread(ts);
}
public void start()
{
killthread = false;
paused = false;
ThreadStart ts = new ThreadStart(() =>
{
Job j = new Job(() => { });
while (true)
{
if (paused)
{
try
{
Thread.Sleep(Timeout.Infinite);
}
catch
{
}
}
else if (killthread)
{
break;
}
else if (jobs.Count != 0)
{
lock (jobs)
{
j = jobs.Dequeue();
}
if (j != null)
{
j.execute();
}
}
else
{
break;
}
}
});
t = new Thread(ts);
t.Start();
}
public void pause()
{
paused = true;
}
public void resume()
{
paused = false;
t.Interrupt();
}
public bool isPaused()
{
return paused;
}
public void killThread()
{
killthread = true;
}
}
}