Creation of dynamic controls - Timers

J

Jerry Spence1

I am trying to create timers on demand by doing the following:

Dim NewTimer As New System.Timers.Timer
NewTimer.Interval = 10000
AddHandler NewTimer.Elapsed, AddressOf Timeup
NewTimer.Enabled = True
NewTimer.Start()

My problem then happens in the TimeUp routine

Normally, to get the name of the control that has been fired you would do:

Private Sub Timeup(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs)

Dim mytimer = DirectCast(sender, Control).Name.ToString

End Sub

The problem is that timers do not have a property called 'Name'. How can I
reference which timer is being fired?

Thanks

-Jerry
 
A

Armin Zingler

Jerry Spence1 said:
I am trying to create timers on demand by doing the following:

Dim NewTimer As New System.Timers.Timer
NewTimer.Interval = 10000
AddHandler NewTimer.Elapsed, AddressOf Timeup
NewTimer.Enabled = True
NewTimer.Start()

My problem then happens in the TimeUp routine

Normally, to get the name of the control that has been fired you
would do:

Private Sub Timeup(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs)

Dim mytimer = DirectCast(sender, Control).Name.ToString

End Sub

The problem is that timers do not have a property called 'Name'. How
can I reference which timer is being fired?


In addition to Cor,

an object can be identified by it's reference. If you don't store the
reference permanently, you can't identify it later. I don't know where you
create the Timer and where you need it. If it's in a Form, store it in a
Form's field instead of in a local variable:

'outside all subs (= a field in the form):
Dim NewTimer As System.Timers.Timer

Later use the 'Is' operator to see if it's a certain timer:

If sender is NewTimer Then
'...
elseif sender is AnotherTimerIfYouHaveOne then
'...
end if


Armin
 
J

Jerry Spence1

Thanks Cor

I'm not sure what you mean by the 'normal' timer. There is a timer in the
components tab, and there is one in the forms tab - both seem to be
system.timers.timer. What's the difference?


-Jerry
 
J

Jerry Spence1

Thanks Armin

If it's reference is stored outside all Subs, then how can I create it
dynamically?

-Jerry
 
J

Jerry Spence1

Forget my last Cor - I've just looked a bit closer. Yes they are different.

Thanks for the example you did - you're the best!

-Jerry
 
A

Armin Zingler

Jerry Spence1 said:
Thanks Armin

If it's reference is stored outside all Subs, then how can I create
it dynamically?

You can add them to an array list.

What I don't understand: If the number of Timers is not known at design
time, the reference is already the "key" to the timer. The 'sender' is
already pointing to the timer that fired the event. Why do you need another
way of identification? I would probably understand it if the number of
Timers was determined at design time. Then you could write code
distinguishing between Timers A, B and C, but as you want to add Timers
dynamically, this is obviously not the case. So, what's your intention?


Armin
 
J

Jerry Spence1

Sorry - my knowledge of VB.Net isn't quite up to scratch to interpret what
you are saying about the 'key' to the timer.

The actual problem is that I have a number of IP based door swipes (access
control). When someone swipes a card, my program receives the card details
and processes it and opens the door (or not). It also creates a component
which receives and records an MPEG4 stream from a video source. That is all
working OK.

I also create a timer and set it to a value which is based on the length of
time that I want the video to be recorded of the person who swiped the card.
When the timer fires, it has to turn off the video stream from that
particular camera. The dynamic part is that I have no idea how many people
are likely to be swiping their cards at the same time, so I need to create
and destroy timers at will. On large installations this could be more than
20 (eg. in a college at home time). When the timer fires it will end up at a
common point, so I need to be able to identify it so that I know which video
stream to cut off.

- Jerry
 
A

Armin Zingler

Jerry Spence1 said:
Sorry - my knowledge of VB.Net isn't quite up to scratch to
interpret what you are saying about the 'key' to the timer.

The actual problem is that I have a number of IP based door swipes
(access control). When someone swipes a card, my program receives
the card details and processes it and opens the door (or not). It
also creates a component which receives and records an MPEG4 stream
from a video source. That is all working OK.

I also create a timer and set it to a value which is based on the
length of time that I want the video to be recorded of the person
who swiped the card. When the timer fires, it has to turn off the
video stream from that particular camera. The dynamic part is that I
have no idea how many people are likely to be swiping their cards at
the same time, so I need to create and destroy timers at will. On
large installations this could be more than 20 (eg. in a college at
home time). When the timer fires it will end up at a common point,
so I need to be able to identify it so that I know which video
stream to cut off.


I don't understand the video part, but... ;-)

.... I think you are looking for an _association_ between the timer and the
video stream. You can inherit from System.Windows.Forms.Timer and add a
property referencing the video stream or whatever object you created for
this purpose.

class VideoTimer
inherits System.Windows.Forms.Timer

public readonly additionaldata as ...

public sub new(byval additionaldata as ...)
me.additionaldata = additionaldata
end sub

end class

Instead of creating a System.Windows.Forms.Timer, create an instance of your
class, and add it to the arraylist I suggested before:

dim vt as videotimer
vt = new VideoTimer(VideoStream)
al.add(vt)


If you get the Timer's event, you can cast to VideoTimer and access the data
related to it:

dim vt as videotimer
vt = directcast(sender, videotimer)
vt.additionaldata.<whatever>



Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top