473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Identify which timer fired event?

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.
Nov 20 '05 #1
7 2124
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 theevent fine, but I cannot identify which timer fired. Is there a way to dothis?
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 AsSystem.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 thecontains the timer. There is no TAG property on the timer. The only valuethat I can seem to identify from the timer is it's interval. The above codeshows "[System.Windows.Forms.Timer], Interval: 100"

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

Nov 20 '05 #2
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

"Grahmmer" <po********@127.0.0.1> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 20 '05 #3
There is no TAG on the timers.

"Sergey Poberezovskiy" <an*******@discussions.microsoft.com> wrote in
message news:03****************************@phx.gbl...
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.
.

Nov 20 '05 #4

"Russ Bishop" <nowhere> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
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...
Nov 20 '05 #5
"Grahammer" <po********@127.0.0.1> wrote...
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
Nov 20 '05 #6
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

"Grahammer" <po********@127.0.0.1> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...

"Russ Bishop" <nowhere> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
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...

Nov 20 '05 #7
"Grahmmer" <po********@127.0.0.1> schrieb
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
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects...
8
by: Daniel P. | last post by:
I'm trying to set a timer that gets called every 3 seconds so I can update a field in the UI with the time elapsed since the process started. What am I doing wrong that timerDF_Tick does not get...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
7
by: Noozer | last post by:
I have a timer on a form. It isn't firing at all. I know that the timer is enabled, and that the interval is low (4000, which should be 4 seconds). To ensure the timer wasn't being inadvertantly...
4
by: Dan | last post by:
Hi, I have a timer on a form (System.Windows.Forms.Timer - Framework 1.1) that is set to 60 seconds as sort an of inactivity monitor. If 60 seconds have elapsed without any user activity I want...
10
by: Bob | last post by:
Okay, I've done this for years but now I'm going to question it just because this idea has been at the back of my head since I started using DotNet... My WinForms app queries a database every 60...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
5
by: Ronin | last post by:
I need a little help trying to figure out the last piece of this puzzle. I've got a form with an associated toolbox that will allow a user to drag a control off the toolbox and drop it onto the...
4
by: Boki | last post by:
Hi All, I have a timer, if my data queue Q has data, the timer should start work, if there is no data in Q, the timer should stop. However, there is an event can fire timer to start. Should I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.