source: trunk/Include/Classes/System/Threading/WaitHandle.ab@ 381

Last change on this file since 381 was 381, checked in by dai, 16 years ago

COMインターフェイスが扱えないデグレを修正。
※COMインターフェイスの定義では必ずIUnkownを継承してください

File size: 3.8 KB
RevLine 
[46]1' Classes/System/Threading/WaitHandle.ab
2
3#ifndef __SYSTEM_THREADING_WAITHANDLE_AB__
4#define __SYSTEM_THREADING_WAITHANDLE_AB__
5
[261]6#require <Classes/System/misc.ab>
[258]7
[261]8Namespace System
9Namespace Threading
10
[258]11Namespace Detail
12 TypeDef PFNSignalObjectAndWait = *Function(hObjectToSignal As HANDLE, hObjectToWaitOn As HANDLE, dwMilliseconds As DWord, bAlertable As DWord) As DWord
13End Namespace
14
[46]15Class WaitHandle
[381]16 Implements System.IDisposable
[46]17Public
[77]18 ' Properties
19' Const Function SafeWaitHandle() As SafeWaitHandle
20' Sub SafeWaitHandle(h As SafeWaitHandle)
[46]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
[77]30 ' Methods
31
32 Virtual Sub ~WaitHandle()
[261]33 Dispose()
[77]34 End Sub
35
[46]36 Virtual Sub Close()
[261]37 Dispose()
[46]38 End Sub
39
[261]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
[47]48 Return WaitOne(INFINITE, FALSE)
49 End Function
[46]50
[261]51 Function WaitOne(millisecondsTimeout As Long, exitContext As BOOL) As Boolean
[47]52 Return WaitHandle.AfterWait(WaitForSingleObject(h, millisecondsTimeout As DWord), 1)
53 End Function
[46]54
[47]55' Function WaitOne(timeout As TimeSpan, exitContext As BOOL) As BOOL
[46]56/*
[47]57 Static Function WaitAll(count As DWord, handles As *HANDLE) As BOOL
58 Return WaitAll(count, handles, INFINITE, FALSE)
59 End Function
[46]60
[47]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
[46]64
[47]65' Static Function WaitAll(timeout As TimeSpan, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
[46]66
[47]67 Static Function WaitAny(count As DWord, handles As *HANDLE) As BOOL
68 Return WaitAny(count, handles, INFINITE, FALSE)
69 End Function
[46]70
[47]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
[46]74
[47]75' Static Function WaitAny(timeout As TimeSpan, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
[46]76*/
[261]77 Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle) As Boolean
[47]78 Return SignalAndWait(toSignal, toWaitOn, INFINITE, FALSE)
79 End Function
[46]80
[258]81Public
[261]82 Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle, millisecondsTimeout As Long, exitContext As BOOL) As Boolean
[258]83 Dim pSignalObjectAndWait = GetProcAddress(GetModuleHandle("Kernel32.dll"), "SignalObjectAndWait") As Detail.PFNSignalObjectAndWait
[46]84 If pSignalObjectAndWait = 0 Then
85 ' PlatformNotSupportedException
86 Debug
[237]87 ExitThread(-1)
[46]88 End If
[237]89 Return WaitHandle.AfterWait(pSignalObjectAndWait(toSignal.Handle, toWaitOn.Handle, millisecondsTimeout As DWord, FALSE), 1)
[47]90 End Function
[46]91
[47]92' Static Function SignalAndWait(ByRef toSignal As WaitHandle, ByRef toWaitOn As WaitHandle, timeout As TimeSpan, exitContext As BOOL) As BOOL
[46]93
94' Protected
95 Sub WaitHandle()
96 End Sub
97
98Protected
99 Static Const InvalidHandle = INVALID_HANDLE_VALUE As HANDLE
100
101Private
102 h As HANDLE
103
[261]104 Static Function AfterWait(ret As DWord, n As DWord) As Boolean
[46]105 Select Case ret
106 Case WAIT_TIMEOUT
[261]107 Return False
[46]108 Case WAIT_ABANDONED
109 ' Throw AbandonedMutexException
110 Debug
111 ExitThread(0)
112 'Case WAIT_FAILED
113 Case Else
[47]114 If WAIT_OBJECT_0 <= ret And ret < WAIT_OBJECT_0 + n Then
[261]115 Return True
[47]116 End If
[46]117 ' ObjectDisposedException?
118 Debug
119 ExitThread(0)
120 End Select
[58]121 End Function
[46]122End Class
123
[261]124End Namespace 'Threading
125End Namespace 'System
[258]126
[46]127#endif '__SYSTEM_THREADING_WAITHANDLE_AB__
Note: See TracBrowser for help on using the repository browser.