CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Timer in Module

  1. #1
    Join Date
    Sep 2000
    Posts
    127

    Timer in Module

    How can i implement a timer in a procedure in one of my modules so that i can impose a delay of 1 second?


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Timer in Module

    Maybe I don't understand your question, but just get some delay, Sleep API would be able to fulfill the job.


  3. #3
    Join Date
    Sep 2000
    Posts
    127

    Re: Timer in Module

    Hi , u mean that sleep api can implement a delay in any procedure in a module (.bas ) file ?


  4. #4
    Join Date
    Apr 2000
    Posts
    737

    Re: Timer in Module

    yap.


  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Timer in Module

    If instead you need a timer event (that means: you need some place where to put code that will be executed form time to time) and you want it without using a timer in a hidden form, then someone else once told me how to do.
    Here is an adaption of his code (sorry man, I do not remember who was)
    '--------
    Module bas code:
    Option Explicit

    Public Declare Function SetTimer Lib "USER32" _
    (ByVal hWnd As Long, ByVal nIDEvent As Long, _
    ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

    Public Declare Function KillTimer Lib "USER32" _
    (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

    Public g_iTimerCounter As Integer
    Public blnTimer As Boolean
    Public lngTimerID As Long

    Public Sub starttimer()
    If blnTimer = False Then
    Call mioTimer(False)
    End If


    End Sub
    Public Sub stoptimer()
    Call mioTimer(True)
    End Sub

    Private Sub mioTimer(ByRef blnTimer As Boolean)

    On Error GoTo ErrHandler

    If blnTimer = False Then
    Debug.Print g_iTimerCounter
    lngTimerID = SetTimer(0, 0, 500, AddressOf TimerProc)
    If lngTimerID = 0 Then
    MsgBox "Timer not created. Ending Program"
    Exit Sub
    End If
    blnTimer = True
    Ftimer.Caption = "Timer started"

    Else
    Debug.Print g_iTimerCounter
    'lngTimerID = 31307
    lngTimerID = KillTimer(0, lngTimerID)
    If lngTimerID = 0 Then
    MsgBox "couldn't kill the timer"
    End If
    g_iTimerCounter = 0
    blnTimer = False
    Ftimer.Caption = "Timer stopped"
    End If
    Exit Sub
    ErrHandler:
    MsgBox Err & Error$
    Resume Next
    End Sub

    Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, _
    ByVal idEvent As Long, ByVal dwTime As Long)

    g_iTimerCounter = g_iTimerCounter + 1
    'timer event: to stop timer here enable this
    'Call mioTimer(True) 'distruggi il timer

    'do your stuff as in timer procedure of a timer

    Debug.Print "FaaaAtto!"
    exit_form_here:
    Screen.MousePointer = vbNormal

    Exit Sub
    ErrHandler:

    Debug.Print Err, Error$

    Resume exit_form_here
    End Sub
    '--------------------------------
    'to test, make a form with two command button (first to start, second to stop
    'the timer), name it Ftimer and paste this code
    'To see event triggered, put a breakpoint in Sub TimerProc
    Option Explicit

    Private Sub Command1_Click()
    MTimer.starttimer
    End Sub

    Private Sub Command2_Click()
    MTimer.stoptimer
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    If blnTimer = True Then
    MTimer.stoptimer
    End If

    End Sub



    Special thanks to Lothar "the Great" Haensler, Tom Archer, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured