Solved

Add a timer programmatically

Posted on 2001-08-03
15
558 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:mcoop
  • 6
  • 3
  • 2
  • +4
15 Comments
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
 

Expert Comment

by:GrahamAtJobserve
Comment Utility
Do you mean at run time or design time?
0
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
in the module(*.bas) you need to use timer api functions and vb callback functions.

I can post some code.
0
 

Expert Comment

by:GrahamAtJobserve
Comment Utility
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
 
LVL 4

Author Comment

by:mcoop
Comment Utility
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
 
LVL 2

Expert Comment

by:bhh
Comment Utility
Listening...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
 
LVL 8

Expert Comment

by:glass_cookie
Comment Utility
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
 
LVL 4

Author Comment

by:mcoop
Comment Utility
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
Comment Utility
0
 
LVL 4

Author Comment

by:mcoop
Comment Utility
Thanks - sorry for the delay, EE has been sick, and I have been busy.

Ended up using your links for a solution.

Regards
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now