|
-
Dec 25th, 2004, 06:58 PM
#1
Thread Starter
Hyperactive Member
Timer in a class collection
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...
-
Dec 25th, 2004, 07:00 PM
#2
Re: Timer in a class collection
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.
-
Dec 25th, 2004, 07:18 PM
#3
Thread Starter
Hyperactive Member
Let me explain....
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?
-
Dec 26th, 2004, 04:06 AM
#4
Thread Starter
Hyperactive Member
Re: Timer in a class collection
-
Dec 26th, 2004, 11:58 AM
#5
Banned
Re: Timer in a class collection
Dim WithEvents CheckTimer As Timer
in the class module
-
Dec 26th, 2004, 12:07 PM
#6
Re: Timer in a class collection
 Originally Posted by MartinLiss
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.
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 timers
-
Dec 26th, 2004, 12:52 PM
#7
Re: Timer in a class collection
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:
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
In the dummy form (frmTimer):
VB Code:
Option Explicit
Public Event TimerEvent()
Private Sub tmrTimer_Timer()
RaiseEvent TimerEvent
End Sub
And finally in your main form (where you make an instance of the class):
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
Last edited by CVMichael; Dec 26th, 2004 at 01:26 PM.
-
Dec 26th, 2004, 01:15 PM
#8
Banned
Re: Timer in a class collection
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
-
Dec 26th, 2004, 01:19 PM
#9
Banned
Re: Timer in a class collection
ok. thats stupid. i cant set the properties so i guess your code works.
-
Dec 26th, 2004, 01:33 PM
#10
Thread Starter
Hyperactive Member
Re: Timer in a class collection
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.
-
Dec 26th, 2004, 01:36 PM
#11
Re: Timer in a class collection
 Originally Posted by Trojan
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.
If I were you, I would stick to the VB timers...
Last edited by CVMichael; Dec 26th, 2004 at 01:49 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|