diff options
Diffstat (limited to 'ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs')
-rw-r--r-- | ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs b/ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs new file mode 100644 index 0000000..3c9c849 --- /dev/null +++ b/ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs | |||
@@ -0,0 +1,82 @@ | |||
1 | using System.Threading; | ||
2 | |||
3 | #if (_WINDOWS_CE) | ||
4 | using System; | ||
5 | using System.Runtime.InteropServices; | ||
6 | #endif | ||
7 | |||
8 | namespace Amib.Threading.Internal | ||
9 | { | ||
10 | /// <summary> | ||
11 | /// EventWaitHandleFactory class. | ||
12 | /// This is a static class that creates AutoResetEvent and ManualResetEvent objects. | ||
13 | /// In WindowCE the WaitForMultipleObjects API fails to use the Handle property | ||
14 | /// of XxxResetEvent. It can use only handles that were created by the CreateEvent API. | ||
15 | /// Consequently this class creates the needed XxxResetEvent and replaces the handle if | ||
16 | /// it's a WindowsCE OS. | ||
17 | /// </summary> | ||
18 | public static class EventWaitHandleFactory | ||
19 | { | ||
20 | /// <summary> | ||
21 | /// Create a new AutoResetEvent object | ||
22 | /// </summary> | ||
23 | /// <returns>Return a new AutoResetEvent object</returns> | ||
24 | public static AutoResetEvent CreateAutoResetEvent() | ||
25 | { | ||
26 | AutoResetEvent waitHandle = new AutoResetEvent(false); | ||
27 | |||
28 | #if (_WINDOWS_CE) | ||
29 | ReplaceEventHandle(waitHandle, false, false); | ||
30 | #endif | ||
31 | |||
32 | return waitHandle; | ||
33 | } | ||
34 | |||
35 | /// <summary> | ||
36 | /// Create a new ManualResetEvent object | ||
37 | /// </summary> | ||
38 | /// <returns>Return a new ManualResetEvent object</returns> | ||
39 | public static ManualResetEvent CreateManualResetEvent(bool initialState) | ||
40 | { | ||
41 | ManualResetEvent waitHandle = new ManualResetEvent(initialState); | ||
42 | |||
43 | #if (_WINDOWS_CE) | ||
44 | ReplaceEventHandle(waitHandle, true, initialState); | ||
45 | #endif | ||
46 | |||
47 | return waitHandle; | ||
48 | } | ||
49 | |||
50 | #if (_WINDOWS_CE) | ||
51 | |||
52 | /// <summary> | ||
53 | /// Replace the event handle | ||
54 | /// </summary> | ||
55 | /// <param name="waitHandle">The WaitHandle object which its handle needs to be replaced.</param> | ||
56 | /// <param name="manualReset">Indicates if the event is a ManualResetEvent (true) or an AutoResetEvent (false)</param> | ||
57 | /// <param name="initialState">The initial state of the event</param> | ||
58 | private static void ReplaceEventHandle(WaitHandle waitHandle, bool manualReset, bool initialState) | ||
59 | { | ||
60 | // Store the old handle | ||
61 | IntPtr oldHandle = waitHandle.Handle; | ||
62 | |||
63 | // Create a new event | ||
64 | IntPtr newHandle = CreateEvent(IntPtr.Zero, manualReset, initialState, null); | ||
65 | |||
66 | // Replace the old event with the new event | ||
67 | waitHandle.Handle = newHandle; | ||
68 | |||
69 | // Close the old event | ||
70 | CloseHandle (oldHandle); | ||
71 | } | ||
72 | |||
73 | [DllImport("coredll.dll", SetLastError = true)] | ||
74 | public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName); | ||
75 | |||
76 | //Handle | ||
77 | [DllImport("coredll.dll", SetLastError = true)] | ||
78 | public static extern bool CloseHandle(IntPtr hObject); | ||
79 | #endif | ||
80 | |||
81 | } | ||
82 | } | ||