forked from EddieDemon/BNSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDispatcher.cs
158 lines (135 loc) · 4.12 KB
/
EventDispatcher.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace BNSharp
{
internal sealed class EventDispatcher : IDisposable
{
private Thread m_low, m_normal;
private EventWaitHandle m_normalBlocker, m_lowBlocker;
private Queue<InvokeHelperBase> m_normalQueue, m_lowQueue;
internal EventDispatcher()
{
m_normalQueue = new Queue<InvokeHelperBase>();
m_lowQueue = new Queue<InvokeHelperBase>();
m_normalBlocker = new EventWaitHandle(false, EventResetMode.AutoReset);
m_lowBlocker = new EventWaitHandle(false, EventResetMode.AutoReset);
m_normal = new Thread(__NormalPriorityThread)
{
Priority = ThreadPriority.BelowNormal,
IsBackground = true
};
m_normal.Start();
m_low = new Thread(__LowPriorityThread)
{
Priority = ThreadPriority.Lowest,
IsBackground = true
};
m_low.Start();
}
public void EnqueueEvent(InvokeHelperBase invokee)
{
m_normalQueue.Enqueue(invokee);
m_normalBlocker.Set();
m_lowQueue.Enqueue(invokee);
}
private void __NormalPriorityThread()
{
try
{
while (true)
{
m_normalBlocker.Reset();
while (m_normalQueue.Count == 0)
{
m_lowBlocker.Set();
m_normalBlocker.WaitOne();
}
if (m_normalQueue.Count > 0)
{
InvokeHelperBase helper = m_normalQueue.Dequeue();
if (helper != null)
helper.Invoke(Priority.Normal);
}
}
}
catch (ThreadAbortException)
{
// exit gracefully
}
}
private void __LowPriorityThread()
{
try
{
while (true)
{
m_lowBlocker.Reset();
while (m_lowQueue.Count == 0 || m_normalQueue.Count > 0)
m_lowBlocker.WaitOne();
if (m_lowQueue.Count > 0)
{
InvokeHelperBase helper = m_lowQueue.Dequeue();
if (helper != null)
{
helper.Invoke(Priority.Low);
helper.FreeArguments();
}
}
}
}
catch (ThreadAbortException)
{
// exit gracefully
}
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~EventDispatcher()
{
Dispose(false);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (m_normal != null)
{
m_normal.Abort();
m_normal = null;
}
if (m_low != null)
{
m_low.Abort();
m_low = null;
}
if (m_normalBlocker != null)
{
m_normalBlocker.Close();
m_normalBlocker = null;
}
if (m_lowBlocker != null)
{
m_lowBlocker.Close();
m_lowBlocker = null;
}
if (m_normalQueue != null)
{
m_normalQueue.Clear();
m_normalQueue = null;
}
if (m_lowQueue != null)
{
m_lowQueue.Clear();
m_lowQueue = null;
}
}
}
#endregion
}
}