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