Identify which timer fired event?

  • Thread starter Thread starter Grahmmer
  • Start date Start date
G

Grahmmer

I have a few timers that are added to a form at runtime. I can handle the
event fine, but I cannot identify which timer fired. Is there a way to do
this?


Timer Creation:
-------------
....some code...
Dim usersTimers(4) As System.Windows.Forms.Timer
For i = 0 To 4
'Build timer
usersTimers(i) = New System.Windows.Forms.Timer
With usersTimers(i)
'.name = "Timer" & Format(i, "00") - No NAME property on dynamic timer.
.Interval = Rand(5000) + 5000 'Five second + random amount
.Start()
End With
AddHandler usersTimers(i).Tick, AddressOf timTick
Next i
....more code...


Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic timer. How to ID?
MsgBox(sender.ToString)
End Sub


Timers don't get added to forms, so you can identify the control the
contains the timer. There is no TAG property on the timer. The only value
that I can seem to identify from the timer is it's interval. The above code
shows "[System.Windows.Forms.Timer], Interval: 100"

P.s. RAND() function generates a random integer between 0 and value,
inclusive.
 
you could try to use Tag propery to hold a unique ID and
then read it in your timTick procedure...
 
Stop for a minute and THINK about what the event signature means.... I'll
wait.

Did ya see it?

If Directcast(Sender, Windows.Forms.Timer) = userTimers(0) Then
Elseif Directcast(Sender, Windows.Forms.Timer) = userTimers(1) Then
End If


The sender is actually a reference to the object that directly fired the
event. Just cast it to a timer and compare the references. Since you can be
guaranteed without fail that Sender will be a timer, then DirectCast is
faster and appropriate. Otherwise you can use CType and trap for an error.
(Also, ctype will look for various possible casts. Directcast will simply
fail if it cannot do a direct and immediate cast.)

-- russ
 
There is no TAG on the timers.

Sergey Poberezovskiy said:
you could try to use Tag propery to hold a unique ID and
then read it in your timTick procedure...
-----Original Message-----
I have a few timers that are added to a form at runtime. I can handle the
event fine, but I cannot identify which timer fired. Is there a way to do
this?


Timer Creation:
-------------
....some code...
Dim usersTimers(4) As System.Windows.Forms.Timer
For i = 0 To 4
'Build timer
usersTimers(i) = New System.Windows.Forms.Timer
With usersTimers(i)
'.name = "Timer" & Format(i, "00") - No NAME property on dynamic timer.
.Interval = Rand(5000) + 5000 'Five second + random amount
.Start()
End With
AddHandler usersTimers(i).Tick, AddressOf timTick
Next i
....more code...


Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic timer. How to ID?
MsgBox(sender.ToString)
End Sub


Timers don't get added to forms, so you can identify the control the
contains the timer. There is no TAG property on the timer. The only value
that I can seem to identify from the timer is it's interval. The above code
shows "[System.Windows.Forms.Timer], Interval: 100"

P.s. RAND() function generates a random integer between 0 and value,
inclusive.


.
 
Russ Bishop said:
Stop for a minute and THINK about what the event signature means.... I'll
wait.

Did ya see it?

If Directcast(Sender, Windows.Forms.Timer) = userTimers(0) Then
Elseif Directcast(Sender, Windows.Forms.Timer) = userTimers(1) Then
End If

ALMOST.... Try:
If Directcast(Sender, Windows.Forms.Timer) IS userTimers(0) Then...

But it makes perfect sense. I just thought it odd that there was no way to
tag a timer, but then again I'm not much of a VB programmer yet.
The sender is actually a reference to the object that directly fired the
event. Just cast it to a timer and compare the references. Since you can be
guaranteed without fail that Sender will be a timer, then DirectCast is
faster and appropriate. Otherwise you can use CType and trap for an error.
(Also, ctype will look for various possible casts. Directcast will simply
fail if it cannot do a direct and immediate cast.)

One question... Is there any reason that I need the Directcast? The
following also works (Only timers will fire this event):
If Sender IS userTimers(0) Then...
 
Grahammer said:
If Directcast(Sender, Windows.Forms.Timer) IS userTimers(0) Then...

BTW you would probably want to assign the first DirectCast to a timer
variable to eliminate casting it for every test. Cast once, test four
times.
One question... Is there any reason that I need the Directcast? The
following also works (Only timers will fire this event):
If Sender IS userTimers(0) Then...

You should only be required to cast sender to a timer if you intend to
reference the properties of the timer.

Tom
 
Grahmmer said:
Dim usersTimers(4) As System.Windows.Forms.Timer
...more code...


Timer Event Handler:
-------------------
Protected Sub timTick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
'Timer ticked. Handle it!
'MsgBox(sender.name) - No NAME property on a dynamic timer. How to
ID? MsgBox(sender.ToString)
End Sub

The sender argument points to the timer. You can compare reference by using
the Is keyword, e.g.

If sender is userTimers(0) Then
 
If you are using Option Strict, you will get a complaint (I think...)

If it works without the cast then don't use it.

-- russ
 
Back
Top