-
Notifications
You must be signed in to change notification settings - Fork 3
/
ThreadPool.cs
69 lines (59 loc) · 2.31 KB
/
ThreadPool.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using UCIS.Util;
namespace UCIS {
public class UThreadPool {
static readonly UThreadPool pManager = new UThreadPool();
public static UThreadPool DefaultPool { get { return pManager; } }
public static void RunTask(WaitCallback Callback, object State) {
DefaultPool.QueueWorkItem(Callback, State);
}
public static void RunCall(WaitCallback Callback, object State) {
DefaultPool.QueueWorkItem(Callback, State);
}
class WorkItem {
public WaitCallback Callback { get; internal set; }
public Object State { get; internal set; }
}
public class ExceptionEventArgs : EventArgs {
public ExceptionEventArgs(Exception exception) {
this.Exception = exception;
}
public Exception Exception { get; private set; }
}
WorkQueue<WorkItem> queue;
public event OnExceptionEventHandler OnException;
public delegate void OnExceptionEventHandler(UThreadPool sender, ExceptionEventArgs e);
public UThreadPool() : this(Environment.ProcessorCount * 100, 0, 5) { }
public UThreadPool(int MaxThreads, int MinIdle, int MaxIdle) {
queue = new WorkQueue<WorkItem>(handler);
queue.UseFrameworkThreadpool = false;
queue.MaxWorkers = MaxThreads;
queue.MaxIdleWorkers = MaxIdle;
queue.MinIdleWorkers = MinIdle;
}
public int ThreadsIdle { get { return queue.IdleWorkers; } }
public int ThreadsBusy { get { return queue.TotalWorkers - queue.IdleWorkers; } }
public int ThreadsAlive { get { return queue.TotalWorkers; } }
public int ThreadsMax { get { return queue.MaxWorkers; } set { queue.MaxWorkers = value; } }
public int ThreadsMaxIdle { get { return queue.MaxIdleWorkers; } set { queue.MaxIdleWorkers = value; } }
public int ThreadsMinIdle { set { queue.MinIdleWorkers = value; } }
public void QueueWorkItem(WaitCallback callback, Object state) {
if (callback == null) throw new ArgumentNullException("callback");
queue.Enqueue(new WorkItem() { Callback = callback, State = state });
}
void handler(WorkItem item) {
try {
item.Callback(item.State);
} catch (Exception ex) {
if (OnException != null) {
OnException(this, new ExceptionEventArgs(ex));
} else {
throw;
}
}
}
}
}