473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s)
'Timer ticked. Handle it!
'MsgBox(sender. name) - No NAME property on a dynamic timer. How to ID?
MsgBox(sender.T oString)
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 2136
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.EventAr gs)
'Timer ticked. Handle it!
'MsgBox(sender. name) - No NAME property on a dynamic timer. How to ID? MsgBox(sender.T oString)
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(Send er, Windows.Forms.T imer) = userTimers(0) Then
Elseif Directcast(Send er, Windows.Forms.T imer) = 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******** ********@TK2MSF TNGP11.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.EventArg s)
'Timer ticked. Handle it!
'MsgBox(sender. name) - No NAME property on a dynamic timer. How to ID?
MsgBox(sender.T oString)
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*******@disc ussions.microso ft.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.EventAr gs)
'Timer ticked. Handle it!
'MsgBox(sender. name) - No NAME property on a dynamic

timer. How to ID?
MsgBox(sender.T oString)
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******** ******@TK2MSFTN GP09.phx.gbl...
Stop for a minute and THINK about what the event signature means.... I'll
wait.

Did ya see it?

If Directcast(Send er, Windows.Forms.T imer) = userTimers(0) Then
Elseif Directcast(Send er, Windows.Forms.T imer) = userTimers(1) Then
End If
ALMOST.... Try:
If Directcast(Send er, Windows.Forms.T imer) 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(Send er, Windows.Forms.T imer) 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******** ******@tk2msftn gp13.phx.gbl...

"Russ Bishop" <nowhere> wrote in message
news:u5******** ******@TK2MSFTN GP09.phx.gbl...
Stop for a minute and THINK about what the event signature means.... I'll wait.

Did ya see it?

If Directcast(Send er, Windows.Forms.T imer) = userTimers(0) Then
Elseif Directcast(Send er, Windows.Forms.T imer) = userTimers(1) Then
End If


ALMOST.... Try:
If Directcast(Send er, Windows.Forms.T imer) 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.EventArg s)
'Timer ticked. Handle it!
'MsgBox(sender. name) - No NAME property on a dynamic timer. How to
ID? MsgBox(sender.T oString)
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
16846
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 interval property inside the event handler? Example follows: Constructor: mTimer = new System.Timers.Timer(20000); mTimer.AutoReset = false;
8
2768
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 called? private System.Windows.Forms.Timer timerDF; this.timerDF = new System.Windows.Forms.Timer(this.components);
9
7252
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; Let's declare a constant Int32 m_TimePeriod = 10000;
7
2369
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 reset I put some extra code in the subs that enable and disable the timer. They fire as expected. To test this I added a second timer with a 1...
4
3322
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 the form to close. I stop/start the timer at button clicks and keydown events etc of the various controls on the form. Users have reported the...
10
1641
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 seconds. I set up a singleton class that's instantiated when the app starts, and that starts a timer. My question is, would it be better to run this...
7
5957
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 seems recently connections just drop off because the timer stops firing. My question is if there is a timeout in the timer event that just shuts...
5
1786
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 form. The form instantiates the control using the Activator.CreateInstance method. What I then need to do (and the part I have not figured out) is...
4
1911
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 add a variable to know how many records inside Q ? Ya, I think so, because timer should use for unknown events, check it in a period.
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7423
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5329
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.