Solved

Add elaspedEvent to System.timer

Posted on 2006-07-06
3
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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