+ Reply to Thread
Results 1 to 5 of 5

A code question from a rookie

  1. #1
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310

    A code question from a rookie

    Please help me with this

    I grabbed the following code from a website, it can be used as a timer. It works exactly as intended.

    My question is which of the variables or procedure names need to be changed to have two or more timers like this coexist in the same workbook.

    I highlighted in bold what I am guessing needs to be changed.


    *********************************************************
    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 TimerID As Long
    Public TimerSeconds As Single

    Sub StartTimer()
    TimerSeconds = 1 ' how often to "pop" the timer.
    TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
    End Sub

    Sub EndTimer()
    On Error Resume Next
    KillTimer 0&, TimerID
    End Sub

    Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
    ByVal nIDEvent As Long, ByVal dwTimer As Long)
    '
    ' The procedure is called by Windows. Put your
    ' timer-related code here.
    '
    End Sub
    **********************************************************
    Here are some comments that were included with the code from the website.

    The procedure TimerProc will be called by Windows every time the timer pops. You can name this procedure anything you want, but you must declare the argument variables exactly as shown in the example. If you change the name of the procedure, be sure to change the name in the call to SetTimer as well.

    nIDEvent The value returned by SetTimer to the TimerID variable. If you have made more than one call to SetTimer, you can examine the nIDEvent argument to determine which call SetTimer to resulted in the procedure being called.

    Thank you for your help

  2. #2
    Andrew B
    Guest

    Re: A code question from a rookie

    Hi
    Try this if what you are trying to do is just delay the running of a
    procedure.


    Application.Wait Now + TimeValue("00:00:05")


    Place this line where you want the delay to occur.

    HTH

    Andrew Bourke


    SuitedAces wrote:
    > Please help me with this
    >
    > I grabbed the following code from a website, it can be used as a timer.
    > It works exactly as intended.
    >
    > My question is which of the variables or procedure names need to be
    > changed to have two or more timers like this coexist in the same
    > workbook.
    >
    > I highlighted in bold what I am guessing needs to be changed.
    >
    >
    > *********************************************************
    > 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 *TimerID* As Long
    > Public *TimerSeconds* As Single
    >
    > Sub *StartTimer*()
    > *TimerSeconds* = 1 ' how often to "pop" the timer.
    > *TimerID* = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf
    > *TimerProc*)
    > End Sub
    >
    > Sub *EndTimer*()
    > On Error Resume Next
    > KillTimer 0&, *TimerID*
    > End Sub
    >
    > Sub *TimerProc*(ByVal HWnd As Long, ByVal uMsg As Long, _
    > ByVal nIDEvent As Long, ByVal dwTimer As Long)
    > '
    > ' The procedure is called by Windows. Put your
    > ' timer-related code here.
    > '
    > End Sub
    > **********************************************************
    > Here are some comments that were included with the code from the
    > website.
    >
    > The procedure TimerProc will be called by Windows every time the timer
    > pops. You can name this procedure anything you want, but you must
    > declare the argument variables exactly as shown in the example. If you
    > change the name of the procedure, be sure to change the name in the
    > call to SetTimer as well.
    >
    > nIDEvent The value returned by SetTimer to the TimerID variable. If
    > you have made more than one call to SetTimer, you can examine the
    > nIDEvent argument to determine which call SetTimer to resulted in the
    > procedure being called.
    >
    > *THANK YOU FOR YOUR HELP*
    >
    >


  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello SuitedAces,

    The API (Applications Programming Interface) timer is identified by the variable nIDEvent in the Function Declaration SetTimer. This is an unsigned integer value (positive number only). The APi is a very useful and powerful interface with few safegaurds. It is important to understand what these calls are doing or you can crash your system.

    Example of 2 Timers:
    Sub StartTimer1()
    TimerSeconds = 1 ' how often to "pop" the timer.
    TimerID = SetTimer(0&, 1&, TimerSeconds * 1000&, AddressOf TimerProc)
    End Sub

    Sub StartTimer2()
    TimerSeconds = 1 ' how often to "pop" the timer.
    TimerID = SetTimer(0&, 2&, TimerSeconds * 1000&, AddressOf TimerProc)
    End Sub

    You should always kill the timer after it is used to free system resources. This kind of housekeeping is automatic in VB but not the API.

    Sincerely,
    Leith Ross

  4. #4
    Forum Contributor
    Join Date
    06-27-2006
    Posts
    310
    Thank you for the replies.

    I have finally been able to make this work consistently , flashing either
    two cell or two labels independently.

    I was able to get OnTime working with one object flashing , but when I went
    to using two timers I ran into all sorts of strange results.

    So I used the API method
    It turns out that I do need to change everything that I outlined in bold.
    On top of that ...
    In my experiment sheet I have four button one to start and one to stop each timer.
    I found out I needed to disable the start button after the first press then
    reenable it with the stop button. Multiple presses of the start buttons gave
    me some irratic behaviour.

    I have the timers being killed in code but I think I will also add it to the sheet close event as a fail safe.

    Thank You for the help

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello SuitedAces,

    I didn't know how you were going to start the timers, but in any case I should have mentioned that calling the start routine again before the timer has finished will restart the cycle. Sorry about that oversight.

    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1