-
Notifications
You must be signed in to change notification settings - Fork 2
/
MTimeOutSupport.bas
159 lines (146 loc) · 5.06 KB
/
MTimeOutSupport.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Attribute VB_Name = "MTimeOutSupport"
Option Explicit
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private m_lngWindowHandle As Long
Private m_intTimeOutObjects As Integer
Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
'
Dim lngObjPointer As Long
Dim objTimeOut As CTimeout
'
On Error Resume Next
'
If Err.Number = 0 Then
'
Set objTimeOut = TimeOutObjectByPointer(idEvent)
'
objTimeOut.PostTimeOutEvent
'
End If
'
End Sub
Public Function RegisterTimer(ByVal lngTimeOutValue As Long, ByVal lngObjPointer As Long) As Long
'
Dim lngElapse As Long
Dim lngEventID As Long
'
lngElapse = CLng(lngTimeOutValue * 1000&)
'
lngEventID = SetTimer(m_lngWindowHandle, lngObjPointer, lngElapse, AddressOf TimerProc)
'
'Debug.Print "TIMER: " & "SetTimer"
'
If lngEventID <> 0 Then
'
RegisterTimer = lngEventID
m_intTimeOutObjects = m_intTimeOutObjects + 1
'
Else
Err.Raise 17002, "MTimeOutSupport.RegisterTimer", "Cannot create the timer"
End If
'
End Function
Public Sub UnRegisterTimer(ByVal lngEventID As Long)
'
Dim lngRetValue As Long
'
lngRetValue = KillTimer(m_lngWindowHandle, lngEventID)
m_intTimeOutObjects = m_intTimeOutObjects - 1
'
'Debug.Print "TIMER: " & "KillTimer"
'
End Sub
Public Sub ResetTimer(ByVal lngEventID As Long, ByVal lngTimeOutValue As Long)
'
Dim lngObjPointer As Long
Dim lngElapse As Long
'
On Error Resume Next
'
lngElapse = CLng(lngTimeOutValue * 1000&)
'
If Err.Number = 0 Then
'
Call SetTimer(m_lngWindowHandle, lngEventID, lngElapse, AddressOf TimerProc)
'Debug.Print "TIMER: " & "SetTimer"
'
End If
'
End Sub
Public Sub CreateTimer()
'
If m_lngWindowHandle = 0 Then
If CreateTimerWindow = 0 Then
Err.Raise 17001, "MTimeOutSupport.CreateTimer", "Cannot create the timer window."
End If
End If
'
End Sub
Public Sub DestroyTimer()
'
Call DestroyTimerWindow
'
End Sub
Private Function CreateTimerWindow() As Long
'********************************************************************************
'Author :Oleg Gdalevich
'Date/Time :17-12-2001
'Purpose :Creates a window to hold timers
'Returns :The window handle
'********************************************************************************
'
'Create a window. We'll not see this window as the ShowWindow is never called.
m_lngWindowHandle = CreateWindowEx(0&, "STATIC", "TIMER_WINDOW", 0&, 0&, 0&, 0&, 0&, 0&, 0&, App.hInstance, ByVal 0&)
'
If m_lngWindowHandle = 0 Then
'
'I really don't know - is it possible? Probably - yes,
'due the lack of the system resources, for example.
'
'In this case the function returns 0.
'
Else
'
'Just to let the caller know that the function was executed successfully
CreateTimerWindow = m_lngWindowHandle
'
'Debug.Print "The timer window is created: " & m_lngWindowHandle
'
End If
'
End Function
Private Function DestroyTimerWindow() As Boolean
'********************************************************************************
'Author :Oleg Gdalevich
'Date/Time :17-12-2001
'Purpose :Destroyes the window
'Returns :If the window was destroyed successfully - True.
'********************************************************************************
'
On Error GoTo ERR_HANDLER
'
'Destroy the window
DestroyWindow m_lngWindowHandle
'
'Debug.Print "The timer window " & m_lngWindowHandle & " is destroyed"
'
'Reset the window handle variable
m_lngWindowHandle = 0
'If no errors occurred, the function returns True
DestroyTimerWindow = True
'
ERR_HANDLER:
End Function
Private Function TimeOutObjectByPointer(ByVal lngObjPointer As Long) As CTimeout
'
Dim objTimeOut As CTimeout
'
CopyMemory objTimeOut, lngObjPointer, 4&
Set TimeOutObjectByPointer = objTimeOut
CopyMemory objTimeOut, 0&, 4&
'
End Function