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