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