Results 1 to 11 of 11

Thread: Timer in a class collection

  1. #1

    Thread Starter
    Hyperactive Member Trojan's Avatar
    Join Date
    Dec 2003
    Location
    Area 51
    Posts
    280

    Unhappy 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...
    MD5How To Detect Disk In Drive X?Time Tickerchanging the owner of a controlSystray HardCore

    • "Programming is like sex: one mistake and you have to support it for the rest of your life."
    • "A program is a spell cast over a computer, turning input into error messages."
    • "WARNING: Keyboard Not Attached. Press F10 to Continue."
    • "Why doesn't DOS ever say 'EXCELLENT command or filename!'"

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member Trojan's Avatar
    Join Date
    Dec 2003
    Location
    Area 51
    Posts
    280

    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?
    MD5How To Detect Disk In Drive X?Time Tickerchanging the owner of a controlSystray HardCore

    • "Programming is like sex: one mistake and you have to support it for the rest of your life."
    • "A program is a spell cast over a computer, turning input into error messages."
    • "WARNING: Keyboard Not Attached. Press F10 to Continue."
    • "Why doesn't DOS ever say 'EXCELLENT command or filename!'"

  4. #4

    Thread Starter
    Hyperactive Member Trojan's Avatar
    Join Date
    Dec 2003
    Location
    Area 51
    Posts
    280

    Re: Timer in a class collection

    anyone?
    MD5How To Detect Disk In Drive X?Time Tickerchanging the owner of a controlSystray HardCore

    • "Programming is like sex: one mistake and you have to support it for the rest of your life."
    • "A program is a spell cast over a computer, turning input into error messages."
    • "WARNING: Keyboard Not Attached. Press F10 to Continue."
    • "Why doesn't DOS ever say 'EXCELLENT command or filename!'"

  5. #5
    Banned
    Join Date
    Dec 2004
    Posts
    174

    Re: Timer in a class collection

    Dim WithEvents CheckTimer As Timer

    in the class module


  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Timer in a class collection

    Quote 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

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Option Explicit
    2.  
    3. Private WithEvents MyTimerFrm As FrmTimer ' frmTimer is your dummy form
    4.  
    5. Private Sub Class_Initialize()
    6.     Set MyTimerFrm = New FrmTimer
    7. End Sub
    8.  
    9. Public Sub StartTimer(ByVal Interval As Integer)
    10.     MyTimerFrm.tmrTimer.Interval = Interval
    11.     MyTimerFrm.tmrTimer.Enabled = True
    12. End Sub
    13.  
    14. Private Sub Class_Terminate()
    15.     Unload MyTimerFrm
    16. End Sub
    17.  
    18. Private Sub MyTimerFrm_TimerEvent()
    19.     Debug.Print "MyTimerFrm_TimerEvent"
    20. End Sub
    In the dummy form (frmTimer):
    VB Code:
    1. Option Explicit
    2.  
    3. Public Event TimerEvent()
    4.  
    5. Private Sub tmrTimer_Timer()
    6.     RaiseEvent TimerEvent
    7. End Sub
    And finally in your main form (where you make an instance of the class):
    VB Code:
    1. Option Explicit
    2.  
    3. Dim C As clsTestTimer
    4.  
    5. Private Sub Form_Load()
    6.     Set C = New clsTestTimer
    7.     C.StartTimer 500
    8. End Sub
    9.  
    10. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    11.     Set C = Nothing
    12. End Sub
    Last edited by CVMichael; Dec 26th, 2004 at 01:26 PM.

  8. #8
    Banned
    Join Date
    Dec 2004
    Posts
    174

    Re: Timer in a class collection

    VB Code:
    1. 'class module
    2. Dim WithEvents Check As Timer
    3.  
    4. Private Sub Check_Timer()
    5.     MsgBox "CHECKED"
    6. End Sub
    7.  
    8. Private Sub Class_Initialize()
    9.     Check.Interval = 10000 '10sec
    10.     Check.Enabled = True
    11. End Sub
    12. 'form
    13. Private Sub Form_Load()
    14.     Dim cls As Class1: Set cls = New Class1
    15. End Sub

    you try it

  9. #9
    Banned
    Join Date
    Dec 2004
    Posts
    174

    Re: Timer in a class collection

    ok. thats stupid. i cant set the properties so i guess your code works.

  10. #10

    Thread Starter
    Hyperactive Member Trojan's Avatar
    Join Date
    Dec 2003
    Location
    Area 51
    Posts
    280

    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.
    MD5How To Detect Disk In Drive X?Time Tickerchanging the owner of a controlSystray HardCore

    • "Programming is like sex: one mistake and you have to support it for the rest of your life."
    • "A program is a spell cast over a computer, turning input into error messages."
    • "WARNING: Keyboard Not Attached. Press F10 to Continue."
    • "Why doesn't DOS ever say 'EXCELLENT command or filename!'"

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Timer in a class collection

    Quote 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
  •  



Click Here to Expand Forum to Full Width