Solved

Add elaspedEvent to System.timer

Posted on 2006-07-06
3
205 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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