Add a timer programmatically

How to adda timer control to a project (module)programmatically...

Of course I would also need to assign events from the 'timer' to a routine that I already have within the module

TIA
LVL 4
mcoopAsked:
Who is Participating?
 
Richie_SimonettiConnect With a Mentor IT OperationsCommented:
0
 
TimCotteeHead of Software ServicesCommented:
You cannot handle events within a standard module, you have to add it to an object module such as a form. Now there are alternatives, if you go to http://www.mvps.org/ccrp and download the ccrp timer you can use this in a class module but still not a standard module.
0
 
GrahamAtJobserveCommented:
Do you mean at run time or design time?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
rkot2000Commented:
in the module(*.bas) you need to use timer api functions and vb callback functions.

I can post some code.
0
 
GrahamAtJobserveCommented:
Can we ask why you wnat to do this? I'm sure that there'll be a better way to acheive what you want.
0
 
Richie_SimonettiIT OperationsCommented:
There is aclass you could use:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CFormlessTimer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
Private Declare Function SetTimer Lib "user32" _
            (ByVal hwnd As Long, _
            ByVal nIDEvent As Long, _
            ByVal uElapse As Long, _
            ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32" _
            (ByVal hwnd As Long, _
            ByVal nIDEvent As Long) As Long

' Member vars:
Private m_Enabled As Boolean
Private m_MilliSeconds As Long
Private m_TimerNumber As Long

Public Event Timer()

Public Sub StopTimer()
If m_TimerNumber <> 0 Then
    KillTimer 0, m_TimerNumber
End If
End Sub

Public Sub StartTimer()
If m_MilliSeconds > 0 Then
    m_TimerNumber = SetTimer(0, 0, m_MilliSeconds, AddressOf TimerProc)
End If
End Sub

Public Property Get Interval() As Long
Interval = m_MilliSeconds
End Property

Public Property Let Interval(ByVal MilliSeconds As Long)
m_MilliSeconds = MilliSeconds
End Property

Public Function TimerEvnt()
Attribute TimerEvnt.VB_MemberFlags = "40"
RaiseEvent Timer
End Function

Private Sub Class_Initialize()
Set modTmrProc.cls = Me
End Sub

Private Sub Class_Terminate()
Call StopTimer
End Sub
0
 
mcoopAuthor Commented:
To answer the earlier questiosn - and perhaps solve my problem...

The code as noted is in a formless application.
... Hence the timer should be created at runtime (i.e. no form to contain the standard timer control)

rkot2 - I thought a module was the answer - but Richie_Simonetti  has suiggested a class - which is a much better idea as it contains all the methods and properties in a 'standard' VB manner...

...but being a class neophyte...

Richie_Simonetti

 - your suggestion looks like what I am after...

I've created the class, but how do I expose the timer object inside my (formless) application ?

e.g.

I named the class 'FormlessTimer'

How to make this work... (startup is Sub main)

sub main()
   FormlessTimer.Interval = 5000
   FormlessTimer.StartTimer
end sub


public sub FormlessTimer_Timer()
  msgbox "formless timer event"
end sub

.. some sample code please ?

I get method or data member not found (at the .Interval=5000) line
0
 
bhhCommented:
Listening...
0
 
Richie_SimonettiIT OperationsCommented:
You need to create an object variable for that class to work:

' in your mmain module
dim tmr as Formlesstimer

sub main
set tmr = new Formlesstimer
tmr.interval=5000
..........


' Don't forget to set that class to nothing when finishes the work
0
 
glass_cookieCommented:
Hi!

If you want to create a 'timer' with no form, you may want to try this:

Use the sleep API, then add the code:

While Not ExitLoop = True
Sleep 30000 /or whatever values you prefer
'Execute code
Wend

That's it!

glass cookie : )

PS. Please pardon me if the VB syntax is wrong as I've been using lots of c++ codes recently.
0
 
mcoopAuthor Commented:
Richie_Simonetti

I'm with you all the way so far - but how do I declare  the 'timer' event in my module...  how i the CformlessTimer bound back to _my_ timer_event 'code' ??


glass_cookie - thanks for the sleep() command but it suspends the thread whereas a timer allows foregrond processing in the task until the timer_event triggers.
0
 
Richie_SimonettiIT OperationsCommented:
I am so sorry! i forgot to post the module that comes with this class. I haven't it right now....!!! it is at my office!
:0
0
 
Richie_SimonettiIT OperationsCommented:
or:

Visual Basic Q&A "How-To" Pages
By Bob Butler



--------------------------------------------------------------------------------


Using a Timer Without a Form {28-May-1999}

The Timer control in VB must be situated on a form in order to be part of
the project. In some cases, notably ActiveX servers, there may not be
any form to act as a host. Although in many cases a form can be loaded
and simply not shown, it is also possible to use a system timer via API
calls without using a control.

Start a new project and place the following code in a module:
Option Explicit
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, _
  ByVal nIDEvent As Long) As Long
Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, _
  ByVal nIDEvent As Long, ByVal uElapse As Long, _
  ByVal lpTimerFunc As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long

Public Function TimerEnable(ByVal hWnd As Long, _
  ByVal TimerID As Long, ByVal mSecs As Long) As Long
TimerEnable = SetTimer(hWnd, TimerID, mSecs, _
  AddressOf TimerFired)
End Function

Public Function TimerDisable(ByVal hWnd As Long, _
  ByVal TimerID As Long) As Long
TimerDisable = KillTimer(hWnd, TimerID)
End Function

Private Sub TimerFired(ByVal hWnd As Long, _
  ByVal TimerID As Long, ByVal IDEvent As Long, _
  ByVal dwTime As Long)
Debug.Print "Fired "; hWnd; TimerID; IDEvent; dwTime
End Sub


Then place this code in a form (just for testing) after creating two
command buttons named cmdStart and cmdStop:
Option Explicit
Private mlTimer1 As Long
Private mlTimer2 As Long

Private Sub cmdStart_Click()
mlTimer1 = TimerEnable(0, 1, 3000)
mlTimer2 = TimerEnable(0, 2, 5000)
Debug.Print "Start 1: "; mlTimer1
Debug.Print "Start 2: "; mlTimer2
End Sub

Private Sub cmdStop_Click()
Debug.Print "Stop: "; TimerDisable(0, mlTimer1)
Debug.Print "Stop: "; TimerDisable(0, mlTimer2)
End Sub


When you run the application you should be able to click the START button
and watch the timer events occurring in the debug window. Clicking STOP
will terminate the timers. There are a few things to watch out for:
1. ALWAYS be sure to stop the timers before stopping the program. If you
don't then the timers will continue to run and your app may appear in the
tasklist after terminating.
2. The return value from the SetTimer call will be the IDEvent value in
the function run when it fires. You can test this value to determine
which timer fired if more than one is started.
3. The function MUST be in a BAS module in VB5 due to restrictions in the
AddressOf operator. If you need to reference it in multiple class or other
modules then your function must know how to re-direct the event to the
appropriate place. An alternative is using a different function for each
SetTimer call.


Special thanks to James Belleau for pointing out a rather serious bug in the
original version of this page.

0
 
mcoopAuthor Commented:
Thanks - sorry for the delay, EE has been sick, and I have been busy.

Ended up using your links for a solution.

Regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.