How can I use a timer i class that is part of a class collection?
can I use more then one timer in a class?
thanks in advanced...
Printable View
How can I use a timer i class that is part of a class collection?
can I use more then one timer in a class?
thanks in advanced...
Some people would argue that one timer is too many, but in any case I think a good rule of thumb is that if you need more than one you should rethink your design.
I am building a task monitor... that is why I need a timer in a class...
Class - Tasks
|
+ - Class - TaskChild
|
+ - TaskChild wait's for 5 min or whatever time I want an checks
something.... and so on...
how can I implement it without putting a timer in a class?
anyone?
Dim WithEvents CheckTimer As Timer
in the class module
:wave:
In my latest client/server application that is about 21,000 lines, I have about 50 timers in total, there are about 25 forms and each form has at least one timer, most of of them with intervals from 100 ms to 1000 ms (not all of them are active at exactly the same time), and everithing works fine... so I really don't understand those people who don't like timersQuote:
Originally Posted by MartinLiss
And about your question Trojan, You CAN'T create a timer in your class, but you CAN make a reference to one.
The code that _visual_basic_ posted, makes a reference to a timer, later on in your code you will have to actually reference the variable to an actual instance of a timer.
[edit] When i actually tried what I just typed previously, it DID NOT WORK !!
I ended up doing the following:
This is what I would do in your case.
make a dummy form (invisible form), and put a timer on it
In your class, do something like:
In the dummy form (frmTimer):VB Code:
Option Explicit Private WithEvents MyTimerFrm As FrmTimer ' frmTimer is your dummy form Private Sub Class_Initialize() Set MyTimerFrm = New FrmTimer End Sub Public Sub StartTimer(ByVal Interval As Integer) MyTimerFrm.tmrTimer.Interval = Interval MyTimerFrm.tmrTimer.Enabled = True End Sub Private Sub Class_Terminate() Unload MyTimerFrm End Sub Private Sub MyTimerFrm_TimerEvent() Debug.Print "MyTimerFrm_TimerEvent" End Sub
And finally in your main form (where you make an instance of the class):VB Code:
Option Explicit Public Event TimerEvent() Private Sub tmrTimer_Timer() RaiseEvent TimerEvent End Sub
VB Code:
Option Explicit Dim C As clsTestTimer Private Sub Form_Load() Set C = New clsTestTimer C.StartTimer 500 End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Set C = Nothing End Sub
VB Code:
'class module Dim WithEvents Check As Timer Private Sub Check_Timer() MsgBox "CHECKED" End Sub Private Sub Class_Initialize() Check.Interval = 10000 '10sec Check.Enabled = True End Sub 'form Private Sub Form_Load() Dim cls As Class1: Set cls = New Class1 End Sub
you try it
ok. thats stupid. i cant set the properties so i guess your code works.
I know of code that usess SetTimer & KillTimer.
The code however uses AddressOf which is bad when you stop your app for debugging...
I'll have to find the code again to review it.
I'll post it here.
It's bad because you have to use subclassing, it is more difficult to do, and if you don't do it the right way, your application will crash often.Quote:
Originally Posted by Trojan
If I were you, I would stick to the VB timers...