DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2005

    How to use timer in a module

    Sorry for my stupid question:
    I want to use a time only in a module, I do not want to add the timer control into a form. How can I define the timer in a module in VB6? Can anybody give an example?
    Last edited by shuiqq; 11-23-2005 at 03:07 PM.

  2. #2
    Join Date
    Nov 2003
    Alameda, CA
    Add this in a bas module:

    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
    Private Sub TimerProc(ByVal hwnd As Long, _
                          ByVal lMsg As Long, _
                          ByVal lTimerID As Long, _
                          ByVal lTimer As Long)
        '' your code here
    End Sub
    To start the timer:
    myTimer = SetTimer(0, 0, millisec, AddressOf TimerProc)

    To stop the timer:
    KillTimer 0, myTimer

    "There are two ways to write error-free programs. Only the third one works."

  3. #3
    Join Date
    Aug 2005
    Thank you, Macro.

    What are meaning of the parameters in the code? Will the subroutine TimerProc() run in background?

    In the function SetTimer(), how can I defined the address of the function TimerProc() in the design time?

    Normally I use VB timer control that has few properties in a form, and it is easy to use. Is it possible to use it in a .bas module?

    Thank you.

  4. #4
    Join Date
    Nov 2003
    Alameda, CA
    VB is strictly single threaded, therefore TimerProc will run in the same thread of the main application, the same way as for the Timer event of the Timer control. For the SetTimer API, look at its defintion in the MSDN documentation (if you do not have MSDN installed, go to www.msdn.com)

    Because SetTimer is an API, VB does not have a design interface for it. Sorry, this is coding 'the old way' (or the right way, like someone thinks)

    Controls can't be added in a bas module. That is, they need a 'container' (like a form) to be created (or 'hosted'). And events can only be handled in cls, frm or ctl files

    Last edited by mstraf; 11-28-2005 at 08:06 PM.
    "There are two ways to write error-free programs. Only the third one works."

Similar Threads

  1. Problem with Timer object
    By teja8100 in forum VB Classic
    Replies: 7
    Last Post: 05-18-2005, 09:44 AM
  2. timer in asp
    By jackal in forum ASP.NET
    Replies: 3
    Last Post: 03-28-2003, 01:29 PM
  3. Timer
    By Tomas in forum ASP.NET
    Replies: 0
    Last Post: 05-22-2002, 01:10 AM
    By Ranger in forum VB Classic
    Replies: 2
    Last Post: 12-08-2000, 08:26 PM
  5. Index server module??
    By Duncan Atack in forum Web
    Replies: 0
    Last Post: 07-13-2000, 10:01 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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center