Solved

How can I create an instance of Timer Control at runtime ?

Posted on 2002-04-19
10
263 Views
Last Modified: 2013-11-26
Hi,

I'd like to have an ActiveX for serial port communication.
The MSComm control is not enough, i need to add a timer.

Because I must create many instance of my ActiveX, I must create some timer instance at runtime.
This code doesn't work...

Private myTimer as Object
...
Set myTimer = CreateObject( "VB.Timer" )

Why ?
Please Help Me

Cordially,

    SB.
0
Comment
Question by:steph_beauquis
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6953394
2 options:
* either you put 1 timer control at design time to your form, and give as index value 0 (i assume the name is MyTimer).
  At runtime, you can then add a new timer using:
  Load myTimer(1)  
  Load myTimer(2)
  etc

* you can, if you have vb6, also use this (inside the form):
  set myTimer = Controls.Add ( "VB.Timer" ,"myTimer", me )

CHeers
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953414
To create a timer dynamically you;'d do:

Private WithEvents mtimer As VB.Timer

Private Sub Command1_Click()
    Set mtimer = Me.Controls.Add("VB.Timer", "tm1")
    mtimer.Interval = 6000
    mtimer.Enabled = True
End Sub

Private Sub mtimer_Timer()
    MsgBox mtimer.Name
End Sub
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953416
WithEvents is the keyword you will need for the coding instance creation.

CJ
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6953480
CJ_S, The only snag I've found using WithEvents is how to make with work with an array.  Have you found a way to make WithEvents work with arrays?
0
 
LVL 5

Accepted Solution

by:
rkot2000 earned 75 total points
ID: 6953514
with activex(dll or exe) am using timer apis
Instead, use these functions in a single code module:

sample with class:
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

     Private m_cb As Object

     Public Function timerSet(lTime As Long, cb As Object) _
          As Long
          Set m_cb = cb
          timerSet = SetTimer(0, 0, lTime, AddressOf _
               timerProcOnce)
     End Function

     Private Sub timerProcOnce(ByVal lHwnd As Long, ByVal _
          lMsg As Long, ByVal lTimerID As Long, ByVal lTime _
          As Long)

          On Error Resume Next
          Call KillTimer(0, lTimerID)
          m_cb.cbTimer
     End Sub

The class module then calls the function like this:
     ...
     timerSet 10, Me
     ...

After 10 milliseconds, the code triggers the cbTimer method in the class module:
Public Sub cbTimer()
     ' Do some stuff
End Sub


sample with regular module :

Insert this code into a bas module:

Dim lTimerId 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 KillTimer Lib “user32” (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

Private Sub TimerProc(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lTimerId As Long, ByVal lTime As Long)
    Dim lResult As Long
    lResult = StopTimer(lTimerId)
    Call InsertYourProcessNameHere 'code to be executed after interval
End Sub
 
Public Sub StartTimer(lInterval As Long) 'convert interval to milliseconds prior to passing
    lTimerId = SetTimer(0, 0, lInterval, AddressOf TimerProc)
End Sub
 
Public Function StopTimer(lTimerId As Long) As Long  'must pass the TimerId returned by SetTimer
    StopTimer = KillTimer(0, lTimerId)
End Function

To execute:

Call StartTimer(5000) '5 seconds


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:inthedark
ID: 6953515
Here are some other possible ways:

1) Add a dummy form to your class and  load a new instance of the form when the class loads. Set a with events reference to the timer.

2) Use the Win32 API call to create a timer event:


=================Sample code

Declare Function SetTimer Lib "user32" _
                           (ByVal hwnd As Long, _
                            ByVal nIDEvent As Long, _
                            ByVal uElapse As Long, _
                            ByVal lpTimerFunc As Long) As Long

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


' Public Variables

Public TimerID As Long



Sub Open()

' Call this to start the timer ops.
   
TimerID=SetTimer( 0, 0, 8000, AddressOf TimerProc)


End Sub

Sub TimerProc()

' Place your timer code here

End Sub

Sub StopTimer()

' call this in the class terminate

if TimerID>0 then
     TimerID = KillTimer(0, TimerID)
end if


End Sub
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953540
inthedark,
Only with custom coding, not with the withevents keyword. :-(

For labels and asuch it doesn't matter you can add them anyway, just like buttons. It's just that one there can only be one eventhandler for any object created with the withevents.

CJ
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6953621
You need to manage index i think. I didn't try it.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6953628
I was thinking same as inthedark but it could be a problem if, and only if, your activex is a dll and not an exe.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953629
Yes, if you do not want to use events. Otherwise it just is not possible. In VB.Net it sure is possible by using delegates but VB6 no...

CJ
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

19 Experts available now in Live!

Get 1:1 Help Now