I have a question for anyone who knows the answer, is it possible to use timer without using a for. This is becaus i write a dll, and at some places things must excecuted at a periodically interver (100 ms)
Printable View
I have a question for anyone who knows the answer, is it possible to use timer without using a for. This is becaus i write a dll, and at some places things must excecuted at a periodically interver (100 ms)
You can check the tickcount on the machine and have your interval accordingly.
The tickcount is the time windows is running on the machine since startup. It is better than timer control and you don't need a form to put it on.
You only have to include the function in a module of the class of your dll.
VB Code:
Public Declare Function GetTickCount Lib "kernel32" () As Long ' put the above line in the general declarations section of a module of your dll 'you can use it like Dim lngTicks As Long lngTicks = GetTickCount ' Your code here Debug.Print GetTickCount - lngTicks
Once an for all, GetTickCount has absolutely ****all to do with a timer :D
Adding a timer in a DLL, is slightly more complicated as you are required to add API commands to you code...
In a module called modTimers, add the following:
Then, in a class module called clsTimerObject:VB Code:
Option Explicit Private mcolTimers As Collection Public Sub Timer_CBK(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal SysTime As Long) Dim objTimer As clsTimerObject Set objTimer = mcolTimers.Item(idEvent & "K") objTimer.RaiseTimerEvent Set objTimer = Nothing End Sub Public Sub AddTimer(ByRef pobjTimer As clsTimerObject) If mcolTimers Is Nothing Then Set mcolTimers = New Collection End If mcolTimers.Add pobjTimer, pobjTimer.Key End Sub Public Sub RemoveTimer(ByVal pstrKey As String) mcolTimers.Remove pstrKey If mcolTimers.Count = 0 Then Set mcolTimers = Nothing End If End Sub
Then in the class you want to have a timer running add the followingVB Code:
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 Public Event Refresh() Private mlngTimerID As Long Friend Property Get Key() Key = mlngTimerID & "K" End Property Public Sub StartTimer(ByVal plngInterval As Long) If mlngTimerID = 0 Then mlngTimerID = SetTimer(0, 0, plngInterval, AddressOf Timer_CBK) AddTimer Me End If End Sub Public Sub StopTimer() If mlngTimerID > 0 Then KillTimer 0, mlngTimerID RemoveTimer Key mlngTimerID = 0 End If End Sub Private Sub Class_Terminate() StopTimer End Sub Friend Sub RaiseTimerEvent() RaiseEvent Refresh End Sub
I hope this helps...VB Code:
Dim WithEvents mobjTimer As clsTimerObject Public Sub StartTimer() Set mobjTimer = New clsTimerObject mobjTimer.StartTimer 1000 'in ms End Sub Private sub mobjTimer_Refresh() 'code here that you want to get repeated every interval End Sub Private Sub Class_Terminate() mcolTimer.StopTimer 'This must be run to terminate the timerobject Set mcolTimer = Nothing
Woka :D
End Sub
Thanx this solved my problem!!!
No probs...:)
If you need a hand with anything else, just give me a shout.
Woka
That DLL does what I have just written, apart from the DLL is about 10 times larger :DQuote:
Woka
Yes. Large is bad. In more instances than one. :DQuote:
Originally posted by Wokawidget
That DLL does what I have just written, apart from the DLL is about 10 times larger :D
Woka