Solved

Add elaspedEvent to System.timer

Posted on 2006-07-06
3
207 Views
Last Modified: 2008-02-01
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.

Assumming

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

thanks
0
Comment
Question by:jaxrpc
3 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17055840
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
   mySubProc()
End Sub
0
 

Author Comment

by:jaxrpc
ID: 17055960
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.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17056025
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
        myTimer.Start()

    End Sub

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

        mySubProc()

    End Sub

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


Fernando
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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