diff options
Diffstat (limited to 'ThirdParty/SmartThreadPool/EventWaitHandle.cs')
-rw-r--r-- | ThirdParty/SmartThreadPool/EventWaitHandle.cs | 206 |
1 files changed, 103 insertions, 103 deletions
diff --git a/ThirdParty/SmartThreadPool/EventWaitHandle.cs b/ThirdParty/SmartThreadPool/EventWaitHandle.cs index 70a1a29..25be07a 100644 --- a/ThirdParty/SmartThreadPool/EventWaitHandle.cs +++ b/ThirdParty/SmartThreadPool/EventWaitHandle.cs | |||
@@ -1,104 +1,104 @@ | |||
1 | #if (_WINDOWS_CE) | 1 | #if (_WINDOWS_CE) |
2 | 2 | ||
3 | using System; | 3 | using System; |
4 | using System.Runtime.InteropServices; | 4 | using System.Runtime.InteropServices; |
5 | using System.Threading; | 5 | using System.Threading; |
6 | 6 | ||
7 | namespace Amib.Threading.Internal | 7 | namespace Amib.Threading.Internal |
8 | { | 8 | { |
9 | /// <summary> | 9 | /// <summary> |
10 | /// EventWaitHandle class | 10 | /// EventWaitHandle class |
11 | /// In WindowsCE this class doesn't exist and I needed the WaitAll and WaitAny implementation. | 11 | /// In WindowsCE this class doesn't exist and I needed the WaitAll and WaitAny implementation. |
12 | /// So I wrote this class to implement these two methods with some of their overloads. | 12 | /// So I wrote this class to implement these two methods with some of their overloads. |
13 | /// It uses the WaitForMultipleObjects API to do the WaitAll and WaitAny. | 13 | /// It uses the WaitForMultipleObjects API to do the WaitAll and WaitAny. |
14 | /// Note that this class doesn't even inherit from WaitHandle! | 14 | /// Note that this class doesn't even inherit from WaitHandle! |
15 | /// </summary> | 15 | /// </summary> |
16 | public class STPEventWaitHandle | 16 | public class STPEventWaitHandle |
17 | { | 17 | { |
18 | #region Public Constants | 18 | #region Public Constants |
19 | 19 | ||
20 | public const int WaitTimeout = Timeout.Infinite; | 20 | public const int WaitTimeout = Timeout.Infinite; |
21 | 21 | ||
22 | #endregion | 22 | #endregion |
23 | 23 | ||
24 | #region Private External Constants | 24 | #region Private External Constants |
25 | 25 | ||
26 | private const Int32 WAIT_FAILED = -1; | 26 | private const Int32 WAIT_FAILED = -1; |
27 | private const Int32 WAIT_TIMEOUT = 0x102; | 27 | private const Int32 WAIT_TIMEOUT = 0x102; |
28 | private const UInt32 INFINITE = 0xFFFFFFFF; | 28 | private const UInt32 INFINITE = 0xFFFFFFFF; |
29 | 29 | ||
30 | #endregion | 30 | #endregion |
31 | 31 | ||
32 | #region WaitAll and WaitAny | 32 | #region WaitAll and WaitAny |
33 | 33 | ||
34 | internal static bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout, bool exitContext) | 34 | internal static bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout, bool exitContext) |
35 | { | 35 | { |
36 | return waitHandle.WaitOne(millisecondsTimeout, exitContext); | 36 | return waitHandle.WaitOne(millisecondsTimeout, exitContext); |
37 | } | 37 | } |
38 | 38 | ||
39 | private static IntPtr[] PrepareNativeHandles(WaitHandle[] waitHandles) | 39 | private static IntPtr[] PrepareNativeHandles(WaitHandle[] waitHandles) |
40 | { | 40 | { |
41 | IntPtr[] nativeHandles = new IntPtr[waitHandles.Length]; | 41 | IntPtr[] nativeHandles = new IntPtr[waitHandles.Length]; |
42 | for (int i = 0; i < waitHandles.Length; i++) | 42 | for (int i = 0; i < waitHandles.Length; i++) |
43 | { | 43 | { |
44 | nativeHandles[i] = waitHandles[i].Handle; | 44 | nativeHandles[i] = waitHandles[i].Handle; |
45 | } | 45 | } |
46 | return nativeHandles; | 46 | return nativeHandles; |
47 | } | 47 | } |
48 | 48 | ||
49 | public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) | 49 | public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) |
50 | { | 50 | { |
51 | uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; | 51 | uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; |
52 | 52 | ||
53 | IntPtr[] nativeHandles = PrepareNativeHandles(waitHandles); | 53 | IntPtr[] nativeHandles = PrepareNativeHandles(waitHandles); |
54 | 54 | ||
55 | int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, true, timeout); | 55 | int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, true, timeout); |
56 | 56 | ||
57 | if (result == WAIT_TIMEOUT || result == WAIT_FAILED) | 57 | if (result == WAIT_TIMEOUT || result == WAIT_FAILED) |
58 | { | 58 | { |
59 | return false; | 59 | return false; |
60 | } | 60 | } |
61 | 61 | ||
62 | return true; | 62 | return true; |
63 | } | 63 | } |
64 | 64 | ||
65 | 65 | ||
66 | public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) | 66 | public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) |
67 | { | 67 | { |
68 | uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; | 68 | uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; |
69 | 69 | ||
70 | IntPtr[] nativeHandles = PrepareNativeHandles(waitHandles); | 70 | IntPtr[] nativeHandles = PrepareNativeHandles(waitHandles); |
71 | 71 | ||
72 | int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, false, timeout); | 72 | int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, false, timeout); |
73 | 73 | ||
74 | if (result >= 0 && result < waitHandles.Length) | 74 | if (result >= 0 && result < waitHandles.Length) |
75 | { | 75 | { |
76 | return result; | 76 | return result; |
77 | } | 77 | } |
78 | 78 | ||
79 | return -1; | 79 | return -1; |
80 | } | 80 | } |
81 | 81 | ||
82 | public static int WaitAny(WaitHandle[] waitHandles) | 82 | public static int WaitAny(WaitHandle[] waitHandles) |
83 | { | 83 | { |
84 | return WaitAny(waitHandles, Timeout.Infinite, false); | 84 | return WaitAny(waitHandles, Timeout.Infinite, false); |
85 | } | 85 | } |
86 | 86 | ||
87 | public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext) | 87 | public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext) |
88 | { | 88 | { |
89 | int millisecondsTimeout = (int)timeout.TotalMilliseconds; | 89 | int millisecondsTimeout = (int)timeout.TotalMilliseconds; |
90 | 90 | ||
91 | return WaitAny(waitHandles, millisecondsTimeout, false); | 91 | return WaitAny(waitHandles, millisecondsTimeout, false); |
92 | } | 92 | } |
93 | 93 | ||
94 | #endregion | 94 | #endregion |
95 | 95 | ||
96 | #region External methods | 96 | #region External methods |
97 | 97 | ||
98 | [DllImport("coredll.dll", SetLastError = true)] | 98 | [DllImport("coredll.dll", SetLastError = true)] |
99 | public static extern int WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool fWaitAll, uint dwMilliseconds); | 99 | public static extern int WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool fWaitAll, uint dwMilliseconds); |
100 | 100 | ||
101 | #endregion | 101 | #endregion |
102 | } | 102 | } |
103 | } | 103 | } |
104 | #endif \ No newline at end of file | 104 | #endif \ No newline at end of file |