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

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.
steph_beauquisAsked:
Who is Participating?
 
rkot2000Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
CJ_SCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
CJ_SCommented:
WithEvents is the keyword you will need for the coding instance creation.

CJ
0
 
inthedarkCommented:
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
 
inthedarkCommented:
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
 
CJ_SCommented:
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
 
Richie_SimonettiIT OperationsCommented:
You need to manage index i think. I didn't try it.
0
 
Richie_SimonettiIT OperationsCommented:
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
 
CJ_SCommented:
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
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.