MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
CancelEvent.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6
7namespace MyCaffe.basecode
8{
16 public class CancelEvent : IDisposable
17 {
18 bool m_bOwnOriginal = true;
19 WaitHandle m_hOriginalCancel = null;
20 List<Tuple<WaitHandle, bool, string>> m_rgCancel = new List<Tuple<WaitHandle, bool, string>>();
21 WaitHandle[] m_rgHandles = new WaitHandle[1];
22 string m_strName = null;
23 object m_syncObj = new object();
24
28 public CancelEvent()
29 {
30 m_hOriginalCancel = new EventWaitHandle(false, EventResetMode.ManualReset, m_strName);
31 setHandles();
32 }
33
34
39 public CancelEvent(string strGlobalName)
40 {
41 if (strGlobalName == null)
42 {
43 m_hOriginalCancel = new EventWaitHandle(false, EventResetMode.ManualReset, m_strName);
44 }
45 else
46 {
47 m_strName = strGlobalName;
48 m_hOriginalCancel = EventWaitHandle.OpenExisting(strGlobalName, System.Security.AccessControl.EventWaitHandleRights.Synchronize | System.Security.AccessControl.EventWaitHandleRights.Modify);
49 }
50
51 setHandles();
52 }
53
58 public CancelEvent(CancelEvent evtCancel)
59 {
60 m_hOriginalCancel = new EventWaitHandle(false, EventResetMode.ManualReset, m_strName);
61 m_rgCancel.Add(new Tuple<WaitHandle, bool, string>(evtCancel.m_hOriginalCancel, false, evtCancel.Name));
62 setHandles();
63 }
64
65 private void setHandles()
66 {
67 lock (m_syncObj)
68 {
69 m_rgHandles = new WaitHandle[m_rgCancel.Count + 1];
70 m_rgHandles[0] = m_hOriginalCancel;
71
72 for (int i = 0; i < m_rgCancel.Count; i++)
73 {
74 m_rgHandles[i + 1] = m_rgCancel[i].Item1;
75 }
76 }
77 }
78
83 public void AddCancelOverride(string strName)
84 {
85 EventWaitHandle evtWait = EventWaitHandle.OpenExisting(strName, System.Security.AccessControl.EventWaitHandleRights.Synchronize | System.Security.AccessControl.EventWaitHandleRights.Modify);
86
87 if (!Contains(strName))
88 {
89 m_rgCancel.Add(new Tuple<WaitHandle, bool, string>(evtWait, true, strName));
90 setHandles();
91 }
92 }
93
98 public void AddCancelOverride(CancelEvent evtCancel)
99 {
100 if (!Contains(evtCancel))
101 {
102 m_rgCancel.Add(new Tuple<WaitHandle, bool, string>(evtCancel.m_hOriginalCancel, false, evtCancel.Name));
103 setHandles();
104 }
105 }
106
111 public void AddCancelOverride(WaitHandle evtCancel)
112 {
113 if (!Contains(evtCancel))
114 {
115 m_rgCancel.Add(new Tuple<WaitHandle, bool, string>(evtCancel, false, null));
116 setHandles();
117 }
118 }
119
125 public bool Contains(CancelEvent evt)
126 {
127 return Contains(evt.m_hOriginalCancel);
128 }
129
135 public bool Contains(WaitHandle evt)
136 {
137 foreach (Tuple<WaitHandle, bool, string> item in m_rgCancel)
138 {
139 if (item.Item1 == evt)
140 return true;
141 }
142
143 return false;
144 }
145
151 public bool Contains(string strName)
152 {
153 foreach (Tuple<WaitHandle, bool, string> item in m_rgCancel)
154 {
155 if (item.Item3 == strName)
156 return true;
157 }
158
159 return false;
160 }
161
167 public bool RemoveCancelOverride(string strName)
168 {
169 int nIdx = -1;
170 bool bResult = false;
171
172 for (int i = 0; i < m_rgCancel.Count; i++)
173 {
174 if (m_rgCancel[i].Item3 == strName)
175 {
176 nIdx = i;
177 break;
178 }
179 }
180
181 if (nIdx >= 0)
182 {
183 if (m_rgCancel[nIdx].Item2)
184 m_rgCancel[nIdx].Item1.Dispose();
185
186 m_rgCancel.RemoveAt(nIdx);
187 bResult = true;
188 }
189
190 setHandles();
191
192 return bResult;
193 }
194
200 public bool RemoveCancelOverride(CancelEvent evtCancel)
201 {
202 int nIdx = -1;
203 bool bResult = false;
204
205 for (int i = 0; i < m_rgCancel.Count; i++)
206 {
207 if (m_rgCancel[i].Item1 == evtCancel.m_hOriginalCancel)
208 {
209 nIdx = i;
210 break;
211 }
212 }
213
214 if (nIdx >= 0)
215 {
216 if (m_rgCancel[nIdx].Item2)
217 m_rgCancel[nIdx].Item1.Dispose();
218
219 m_rgCancel.RemoveAt(nIdx);
220 bResult = true;
221 }
222
223 setHandles();
224
225 return bResult;
226 }
227
233 public bool RemoveCancelOverride(WaitHandle evtCancel)
234 {
235 bool bResult = false;
236
237 int nIdx = -1;
238
239 for (int i = 0; i < m_rgCancel.Count; i++)
240 {
241 if (m_rgCancel[i].Item1 == evtCancel)
242 {
243 nIdx = i;
244 break;
245 }
246 }
247
248 if (nIdx >= 0)
249 {
250 m_rgCancel.RemoveAt(nIdx);
251 bResult = true;
252 }
253
254 setHandles();
255
256 return bResult;
257 }
258
262 public string Name
263 {
264 get { return m_strName; }
265 }
266
270 public void Set()
271 {
272 if (m_hOriginalCancel is EventWaitHandle)
273 ((EventWaitHandle)m_hOriginalCancel).Set();
274 }
275
279 public void Reset()
280 {
281 if (m_hOriginalCancel is EventWaitHandle)
282 ((EventWaitHandle)m_hOriginalCancel).Reset();
283 }
284
290 public bool WaitOne(int nMs = int.MaxValue)
291 {
292 if (WaitHandle.WaitAny(Handles, nMs) == WaitHandle.WaitTimeout)
293 return false;
294
295 return true;
296 }
297
301 public WaitHandle[] Handles
302 {
303 get
304 {
305 lock (m_syncObj)
306 {
307 return m_rgHandles;
308 }
309 }
310 }
311
312 #region IDisposable Support
313
318 protected virtual void Dispose(bool disposing)
319 {
320 if (m_bOwnOriginal && m_hOriginalCancel != null)
321 {
322 m_hOriginalCancel.Dispose();
323 m_hOriginalCancel = null;
324 }
325
326 foreach (Tuple<WaitHandle, bool, string> item in m_rgCancel)
327 {
328 if (item.Item2)
329 item.Item1.Dispose();
330 }
331
332 m_rgCancel.Clear();
333 m_rgHandles = null;
334 }
335
339 public void Dispose()
340 {
341 Dispose(true);
342 }
343 #endregion
344 }
345}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
CancelEvent(CancelEvent evtCancel)
Create a new Cancel Event and add another to this ones overrides.
Definition: CancelEvent.cs:58
void Reset()
Resets the event clearing any signaled state.
Definition: CancelEvent.cs:279
bool Contains(string strName)
Check to see if the named cancel event has already been added.
Definition: CancelEvent.cs:151
CancelEvent(string strGlobalName)
The CancelEvent constructor that accepts a global name.
Definition: CancelEvent.cs:39
WaitHandle[] Handles
Returns the internal wait handle of the CancelEvent.
Definition: CancelEvent.cs:302
bool RemoveCancelOverride(string strName)
Remove a new cancel override.
Definition: CancelEvent.cs:167
bool RemoveCancelOverride(WaitHandle evtCancel)
Remove a new cancel override.
Definition: CancelEvent.cs:233
virtual void Dispose(bool disposing)
Releases all resources used by the CancelEvent.
Definition: CancelEvent.cs:318
void AddCancelOverride(string strName)
Add a new cancel override.
Definition: CancelEvent.cs:83
string Name
Return the name of the cancel event.
Definition: CancelEvent.cs:263
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
Definition: CancelEvent.cs:290
CancelEvent()
The CancelEvent constructor.
Definition: CancelEvent.cs:28
void Set()
Sets the event to the signaled state.
Definition: CancelEvent.cs:270
bool RemoveCancelOverride(CancelEvent evtCancel)
Remove a new cancel override.
Definition: CancelEvent.cs:200
void AddCancelOverride(CancelEvent evtCancel)
Add a new cancel override.
Definition: CancelEvent.cs:98
void Dispose()
Releases all resources used by the CancelEvent.
Definition: CancelEvent.cs:339
void AddCancelOverride(WaitHandle evtCancel)
Add a new cancel override.
Definition: CancelEvent.cs:111
bool Contains(CancelEvent evt)
Check to see if the cancel event has already been added.
Definition: CancelEvent.cs:125
bool Contains(WaitHandle evt)
Check to see if the cancel event has already been added.
Definition: CancelEvent.cs:135
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12