• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Add elaspedEvent to System.timer

Hi, i have created a Dim myTimer as new System.timer(1000) .

Now i have a function which i wants the timer to call every 1sec. How do i write the code for tell the timer which function to call when there's an elapsedEvent.


Dim myTimer as New System.timer(1000) ' my timer created at run-time

Public Sub mySubProc()
'Do some stuffs here every 1 sec
End Sub

1 Solution
First you have to start the timer, such as with a button click:

Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
     myTimer.Enabled = True
End Sub

Then you have to catch the Timer tick event:

Private Sub myTimer_Tick(sender As Object, e As System.EventArgs) Handles myTimer.Tick
End Sub
jaxrpcAuthor Commented:
hmm what if i want to do it programmatically, like adding delegate event programmatically.

like initialise a new system.timer()
and when it starts it will call an event.

And i might need to initalise an array of timers.
Fernando SotoRetiredCommented:
Hi jaxrpc;

This sample code will show you how to do it.

    ' Reference to Timer
    Private myTimer As Timer

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Create timer in code
        myTimer = New Timer
        myTimer.Interval = 1000
        myTimer.Enabled = True
        ' Add Handler for the tick event
        AddHandler myTimer.Tick, AddressOf myTimer_Tick

    End Sub

    ' Tick event handler for the timer
    Private Sub myTimer_Tick(ByVal sender As Object, _
        ByVal e As System.EventArgs)


    End Sub

    ' Function timer calles.
    Public Sub mySubProc()
        'Do some stuffs here every 1 sec
    End Sub

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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now