Results 1 to 8 of 8

Thread: Timer without a form

  1. #1

    Thread Starter
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Unhappy Timer without a form

    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)

  2. #2
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    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:
    1. Public Declare Function GetTickCount Lib "kernel32" () As Long
    2. ' put the above line in the general declarations section of a module of your dll
    3.  
    4. 'you can use it like
    5. Dim lngTicks As Long
    6.  
    7.     lngTicks = GetTickCount
    8.     ' Your code here
    9.     Debug.Print GetTickCount - lngTicks
    Last edited by swatty; Nov 4th, 2002 at 07:06 AM.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Once an for all, GetTickCount has absolutely ****all to do with a timer
    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:
    VB Code:
    1. Option Explicit
    2.  
    3. Private mcolTimers As Collection
    4.  
    5. Public Sub Timer_CBK(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal SysTime As Long)
    6. Dim objTimer   As clsTimerObject
    7.     Set objTimer = mcolTimers.Item(idEvent & "K")
    8.     objTimer.RaiseTimerEvent
    9.     Set objTimer = Nothing
    10. End Sub
    11.  
    12. Public Sub AddTimer(ByRef pobjTimer As clsTimerObject)
    13.    If mcolTimers Is Nothing Then
    14.       Set mcolTimers = New Collection
    15.    End If
    16.    mcolTimers.Add pobjTimer, pobjTimer.Key
    17. End Sub
    18.  
    19. Public Sub RemoveTimer(ByVal pstrKey As String)
    20.     mcolTimers.Remove pstrKey
    21.     If mcolTimers.Count = 0 Then
    22.         Set mcolTimers = Nothing
    23.     End If
    24. End Sub
    Then, in a class module called clsTimerObject:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    4. Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    5.  
    6. Public Event Refresh()
    7.  
    8. Private mlngTimerID      As Long
    9.  
    10. Friend Property Get Key()
    11.     Key = mlngTimerID & "K"
    12. End Property
    13.  
    14. Public Sub StartTimer(ByVal plngInterval As Long)
    15.     If mlngTimerID = 0 Then
    16.         mlngTimerID = SetTimer(0, 0, plngInterval, AddressOf Timer_CBK)
    17.         AddTimer Me
    18.     End If
    19. End Sub
    20.  
    21. Public Sub StopTimer()
    22.     If mlngTimerID > 0 Then
    23.         KillTimer 0, mlngTimerID
    24.         RemoveTimer Key
    25.         mlngTimerID = 0
    26.     End If
    27. End Sub
    28.  
    29. Private Sub Class_Terminate()
    30.     StopTimer
    31. End Sub
    32.  
    33. Friend Sub RaiseTimerEvent()
    34.     RaiseEvent Refresh
    35. End Sub
    Then in the class you want to have a timer running add the following
    VB Code:
    1. Dim WithEvents mobjTimer   As clsTimerObject
    2.  
    3. Public Sub StartTimer()
    4.     Set mobjTimer = New clsTimerObject
    5.     mobjTimer.StartTimer 1000 'in ms
    6. End Sub
    7.  
    8. Private sub mobjTimer_Refresh()
    9.    'code here that you want to get repeated every interval
    10. End Sub
    11.  
    12. Private Sub Class_Terminate()
    13.    mcolTimer.StopTimer 'This must be run to terminate the timerobject
    14.    Set mcolTimer = Nothing
    I hope this helps...


    Woka
    End Sub

  4. #4

    Thread Starter
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Solved

    Thanx this solved my problem!!!

  5. #5

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Wokawidget
    That DLL does what I have just written, apart from the DLL is about 10 times larger

    Woka
    Yes. Large is bad. In more instances than one.
    My evil laugh has a squeak in it.

    kristopherwilson.com

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