1 | ' Classes/System/Threading/WaitHandle.ab
|
---|
2 |
|
---|
3 | #ifndef __SYSTEM_THREADING_WAITHANDLE_AB__
|
---|
4 | #define __SYSTEM_THREADING_WAITHANDLE_AB__
|
---|
5 |
|
---|
6 | 'Namespace System
|
---|
7 | 'Namespace Threading
|
---|
8 |
|
---|
9 | Namespace Detail
|
---|
10 | TypeDef PFNSignalObjectAndWait = *Function(hObjectToSignal As HANDLE, hObjectToWaitOn As HANDLE, dwMilliseconds As DWord, bAlertable As DWord) As DWord
|
---|
11 | End Namespace
|
---|
12 |
|
---|
13 | Class WaitHandle
|
---|
14 | Public
|
---|
15 | ' Properties
|
---|
16 | ' Const Function SafeWaitHandle() As SafeWaitHandle
|
---|
17 | ' Sub SafeWaitHandle(h As SafeWaitHandle)
|
---|
18 |
|
---|
19 | Const Virtual Function Handle() As HANDLE
|
---|
20 | Return h
|
---|
21 | End Function
|
---|
22 |
|
---|
23 | Virtual Sub Handle(newHandle As HANDLE)
|
---|
24 | h = newHandle
|
---|
25 | End Sub
|
---|
26 |
|
---|
27 | ' Methods
|
---|
28 |
|
---|
29 | Virtual Sub ~WaitHandle()
|
---|
30 | CloseHandle(h)
|
---|
31 | End Sub
|
---|
32 |
|
---|
33 | Virtual Sub Close()
|
---|
34 | CloseHandle(h)
|
---|
35 | End Sub
|
---|
36 |
|
---|
37 | Function WaitOne() As BOOL
|
---|
38 | Return WaitOne(INFINITE, FALSE)
|
---|
39 | End Function
|
---|
40 |
|
---|
41 | Function WaitOne(millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
42 | Return WaitHandle.AfterWait(WaitForSingleObject(h, millisecondsTimeout As DWord), 1)
|
---|
43 | End Function
|
---|
44 |
|
---|
45 | ' Function WaitOne(timeout As TimeSpan, exitContext As BOOL) As BOOL
|
---|
46 | /*
|
---|
47 | Static Function WaitAll(count As DWord, handles As *HANDLE) As BOOL
|
---|
48 | Return WaitAll(count, handles, INFINITE, FALSE)
|
---|
49 | End Function
|
---|
50 |
|
---|
51 | Static Function WaitAll(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
52 | Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, TRUE, millisecondsTimeout), count)
|
---|
53 | End Function
|
---|
54 |
|
---|
55 | ' Static Function WaitAll(timeout As TimeSpan, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
56 |
|
---|
57 | Static Function WaitAny(count As DWord, handles As *HANDLE) As BOOL
|
---|
58 | Return WaitAny(count, handles, INFINITE, FALSE)
|
---|
59 | End Function
|
---|
60 |
|
---|
61 | Static Function WaitAny(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
62 | Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, FALSE, millisecondsTimeout), count)
|
---|
63 | End Function
|
---|
64 |
|
---|
65 | ' Static Function WaitAny(timeout As TimeSpan, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
66 | */
|
---|
67 | Static Function SignalAndWait(ByRef toSignal As WaitHandle, ByRef toWaitOn As WaitHandle) As BOOL
|
---|
68 | Return SignalAndWait(toSignal, toWaitOn, INFINITE, FALSE)
|
---|
69 | End Function
|
---|
70 |
|
---|
71 | Public
|
---|
72 | Static Function SignalAndWait(ByRef toSignal As WaitHandle, ByRef toWaitOn As WaitHandle, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
73 | Dim pSignalObjectAndWait = GetProcAddress(GetModuleHandle("Kernel32.dll"), "SignalObjectAndWait") As Detail.PFNSignalObjectAndWait
|
---|
74 | If pSignalObjectAndWait = 0 Then
|
---|
75 | ' PlatformNotSupportedException
|
---|
76 | Debug
|
---|
77 | ExitThread(-1)
|
---|
78 | End If
|
---|
79 | Return WaitHandle.AfterWait(pSignalObjectAndWait(toSignal.Handle, toWaitOn.Handle, millisecondsTimeout As DWord, FALSE), 1)
|
---|
80 | End Function
|
---|
81 |
|
---|
82 | ' Static Function SignalAndWait(ByRef toSignal As WaitHandle, ByRef toWaitOn As WaitHandle, timeout As TimeSpan, exitContext As BOOL) As BOOL
|
---|
83 |
|
---|
84 | ' Protected
|
---|
85 | Sub WaitHandle()
|
---|
86 | End Sub
|
---|
87 |
|
---|
88 | Protected
|
---|
89 | Static Const InvalidHandle = INVALID_HANDLE_VALUE As HANDLE
|
---|
90 |
|
---|
91 | Private
|
---|
92 | h As HANDLE
|
---|
93 |
|
---|
94 | Static Function AfterWait(ret As DWord, n As DWord) As BOOL
|
---|
95 | Select Case ret
|
---|
96 | Case WAIT_TIMEOUT
|
---|
97 | Return FALSE
|
---|
98 | Case WAIT_ABANDONED
|
---|
99 | ' Throw AbandonedMutexException
|
---|
100 | Debug
|
---|
101 | ExitThread(0)
|
---|
102 | 'Case WAIT_FAILED
|
---|
103 | Case Else
|
---|
104 | If WAIT_OBJECT_0 <= ret And ret < WAIT_OBJECT_0 + n Then
|
---|
105 | Return TRUE
|
---|
106 | End If
|
---|
107 | ' ObjectDisposedException?
|
---|
108 | Debug
|
---|
109 | ExitThread(0)
|
---|
110 | End Select
|
---|
111 | End Function
|
---|
112 | End Class
|
---|
113 |
|
---|
114 | 'End Namespace 'Threading
|
---|
115 | 'End Namespace 'System
|
---|
116 |
|
---|
117 | #endif '__SYSTEM_THREADING_WAITHANDLE_AB__
|
---|