Multi timer

simian

Member
Joined
Jun 7, 2006
Messages
13
Programming Experience
Beginner
I am writting a program that needs to fire seperate timers at different interval. The problem I have is that there is a user defind number of timers and user defind intervals. Can any one help me figure out how to do this.
Thanks in advance Simon.
 
Can you give some more detail about what you want information on.

1. How to create a timer in run-time
2. How to set timer properties such as interval

Glad to help
 
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
  Dim t As New Timer
  t.Interval = 1000
  AddHandler t.Tick, AddressOf AnyTimer_Tick
  t.Start()
End Sub
 
Dim ix As Integer
 
Private Sub AnyTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
  ix += 1
  Me.Text = ix.ToString
End Sub
 
Here is also one example with an array of timers and different event handlers and stopping all the timers with a button:
VB.NET:
Dim allmytimers(2) As Timer
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
  Dim t As New Timer
  t.Tag = "mytimer1"
  t.Interval = 1000
  AddHandler t.Tick, AddressOf AnyTimer2_Tick
  t.Start()
  allmytimers(0) = t
 
  t = New Timer
  t.Tag = "mytimer2"
  t.Interval = 1500
  AddHandler t.Tick, AddressOf AnyTimer2_Tick
  t.Start()
  allmytimers(1) = t
 
  t = New Timer
  t.Interval = 400
  AddHandler t.Tick, AddressOf AnyTimer3_Tick
  t.Start()
  allmytimers(2) = t
End Sub
 
Dim ix As Integer
 
Private Sub AnyTimer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
  ix += 1
  Me.Text = DirectCast(sender, Timer).Tag.ToString & " - " & ix.ToString
End Sub
 
Private Sub AnyTimer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
  ix += 1
  Me.Text = "timer 3 - " & ix.ToString
End Sub
 
Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
  For Each t As Timer In allmytimers
    t.Stop()
  Next
End Sub
 
Back
Top