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