-
how to pass a variable to a timer's handler?
i need to know how to pass a variable (eg. string) over to a handler. i need
the variable in order to perform some variable manipulation.. thank you
--
AddHandler aTimer.Elapsed, AddressOf OnTimer
--
Public Sub OnTimer(ByVal source As Object, ByVal e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
-
Re: how to pass a variable to a timer's handler?
"brandon" <brandonheng@hotmail.com> wrote in
news:3db9c36f$1@tnews.web.devx.com:
A timer event occurs in the context of the instance to which it is bound.
In other words, just let the OnTimer method access a member variable. No
magic required.
> i need to know how to pass a variable (eg. string) over to a handler.
> i need the variable in order to perform some variable manipulation..
> thank you --
> AddHandler aTimer.Elapsed, AddressOf OnTimer
> --
> Public Sub OnTimer(ByVal source As Object, ByVal e As
> ElapsedEventArgs)
> Console.WriteLine("Hello World!")
> End Sub
>
>
>
>
--
Rune Bivrin
- OOP since 1989
- SQL Server since 1990
- VB since 1991
-
Re: how to pass a variable to a timer's handler?
i've got problem doing that..
here's a snippet of the code..
--
Dim msg As String = "hi, how are you?"
--
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer()
Dim cut As String = "you"
AddHandler aTimer.Elapsed, AddressOf OnTimer()
aTimer.Interval = 5000
aTimer.Enabled = True
End Sub
---
Public Sub OnTimer(ByVal source As Object, ByVal e As ElapsedEventArgs)
msg = Replace(msg, cut, "everyone")
TextBox1.AppendText(msg)
source.enabled = False
End Sub
---
how do i pass the 'cut' to the ontimer event?
"Rune Bivrin" <rune@bivrin.com> wrote in message
news:Xns92B36BF8472Erunebivrincom@209.1.14.29...
> "brandon" <brandonheng@hotmail.com> wrote in
> news:3db9c36f$1@tnews.web.devx.com:
>
> A timer event occurs in the context of the instance to which it is bound.
> In other words, just let the OnTimer method access a member variable. No
> magic required.
>
-
Re: how to pass a variable to a timer's handler?
"brandon" <brandonheng@hotmail.com> wrote in
news:3dba6cab@tnews.web.devx.com:
Well, the simple answer is just declare cut just after msg, rather than
inside Button1_Click.
The complex answer is it all depends on when and why cut and/or msg
changes.
> i've got problem doing that..
> here's a snippet of the code..
> --
> Dim msg As String = "hi, how are you?"
>
> --
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
> Dim aTimer As System.Timers.Timer
>
> aTimer = New System.Timers.Timer()
>
> Dim cut As String = "you"
>
> AddHandler aTimer.Elapsed, AddressOf OnTimer()
>
> aTimer.Interval = 5000
>
> aTimer.Enabled = True
>
> End Sub
>
> ---
>
> Public Sub OnTimer(ByVal source As Object, ByVal e As
> ElapsedEventArgs)
>
> msg = Replace(msg, cut, "everyone")
>
> TextBox1.AppendText(msg)
>
> source.enabled = False
>
> End Sub
>
> ---
>
> how do i pass the 'cut' to the ontimer event?
>
>
>
> "Rune Bivrin" <rune@bivrin.com> wrote in message
> news:Xns92B36BF8472Erunebivrincom@209.1.14.29...
>> "brandon" <brandonheng@hotmail.com> wrote in
>> news:3db9c36f$1@tnews.web.devx.com:
>>
>> A timer event occurs in the context of the instance to which it is
>> bound. In other words, just let the OnTimer method access a member
>> variable. No magic required.
>>
>
>
>
>
--
Rune Bivrin
- OOP since 1989
- SQL Server since 1990
- VB since 1991
-
Re: how to pass a variable to a timer's handler?
There are two ways that spring to mind:
1. If you only have one timer running at any time, make 'cut' a global
variable as you have with 'msg'. Messy, but would work.
2. (and this is neat, really showing what you can do with .Net!)
Create a class to handle the event, setting the variables to any values you
need (like your 'cut' variable)
Point your timer to this instance of the class.
Here is a sample I wrote:
Public Class Form1
Inherits System.Windows.Forms.Form
Delegate Sub OnTimerHandler(ByVal source As Object, ByVal e As
EventArgs)
#Region " Windows Form Designer generated code "
'Create a form with a Button
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
AddHandler Timer1.Tick, AddressOf New TimerHandler("First
timer!").OnTimer
Timer1.Enabled = True
End Sub
End Class
Public Class TimerHandler
Private StringToShow As String
Sub New(ByVal pStringToShow As String)
StringToShow = pStringToShow
End Sub
Public Sub OnTimer(ByVal source As Object, ByVal e As EventArgs)
CType(source, Windows.Forms.Timer).Enabled = False
MsgBox("Got timer: " + StringToShow)
End Sub
End Class
The beauty of this is that you can create as many timers as you like and
have each point to its own class to do whatever you like.
The line AddHandler Timer1.Tick, AddressOf New TimerHandler("First
timer!").OnTimer shows how easy this is to do.
If you want to be _really_ fancy, here is some code to create dynamic timers
and handlers, triggering events, and then removing the event handlers
automatically:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
'Create a form with a Button
#End Region
Delegate Sub OnTimerHandler(ByVal source As Object, ByVal e As
System.Timers.ElapsedEventArgs)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
AddHandler New MyTimer(1000).Elapsed, AddressOf New TimerHandler("First
timer after 1000!").OnTimer
AddHandler New MyTimer(2000).Elapsed, AddressOf New TimerHandler("First
timer after 2000!").OnTimer
AddHandler New MyTimer(2500).Elapsed, AddressOf New TimerHandler("First
timer after 2500!").OnTimer
AddHandler New MyTimer(3000).Elapsed, AddressOf New TimerHandler("First
timer after 3000!").OnTimer
End Sub
End Class
Public Class TimerHandler
Private StringToShow As String
Sub New(ByVal pStringToShow As String)
StringToShow = pStringToShow
End Sub
Public Sub OnTimer(ByVal source As Object, ByVal e As
System.Timers.ElapsedEventArgs)
CType(source, System.Timers.Timer).Enabled = False
RemoveHandler CType(source, MyTimer).Elapsed, AddressOf Me.OnTimer
MsgBox("Got timer: " + StringToShow)
End Sub
End Class
Public Class MyTimer : Inherits System.Timers.Timer
Sub New(ByVal pInterval As Integer)
Me.Interval = pInterval
Me.Enabled = True
End Sub
End Class
Hmm.. This looks so useful that perhaps I should stick this up on the web
somewhere.
Hope this helps you a bit.
Cheers,
iGadget
"brandon" <brandonheng@hotmail.com> wrote in message
news:3dba6cab@tnews.web.devx.com...
> i've got problem doing that..
> here's a snippet of the code..
> --
> Dim msg As String = "hi, how are you?"
>
> --
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
> Dim aTimer As System.Timers.Timer
>
> aTimer = New System.Timers.Timer()
>
> Dim cut As String = "you"
>
> AddHandler aTimer.Elapsed, AddressOf OnTimer()
>
> aTimer.Interval = 5000
>
> aTimer.Enabled = True
>
> End Sub
>
> ---
>
> Public Sub OnTimer(ByVal source As Object, ByVal e As ElapsedEventArgs)
>
> msg = Replace(msg, cut, "everyone")
>
> TextBox1.AppendText(msg)
>
> source.enabled = False
>
> End Sub
>
> ---
>
> how do i pass the 'cut' to the ontimer event?
>
>
>
> "Rune Bivrin" <rune@bivrin.com> wrote in message
> news:Xns92B36BF8472Erunebivrincom@209.1.14.29...
> > "brandon" <brandonheng@hotmail.com> wrote in
> > news:3db9c36f$1@tnews.web.devx.com:
> >
> > A timer event occurs in the context of the instance to which it is
bound.
> > In other words, just let the OnTimer method access a member variable. No
> > magic required.
> >
>
>
>
-
Re: how to pass a variable to a timer's handler?
well, let me tell you my scenario to make understandings easier..
i am building a client/server system.. when client[s] connects to the
server, particular information will be displayed on server
user(from server) may click the client's info from treeview and it will send
a request to client.. asking for the updated info.. then update the central
database..
problem is we dun wan the user( from server) to send the request many times,
when the user starts clicking.. right? so, i set a timer on the treeview
afterselect..
since there will be many clients connected to the server.. it will
inappropriate to use boolean variables to access the sending request code..
instead.. i tried to use strings where if i instr the variable for the pc
name.. if dun have then it'll send the request and add the client's ID to
the list.. if have it will skip..
at the ontimer event.. it will get the list of clients already sent the
request.. and get the string on which client called it.. it will remove the
client's ID from the list of clients' ID..
the string(stringManyClients) containing the list of clients' ID can be
delclared as public.. we need to declare a string(MyString) for the timer to
compare the list of clients(stringManyClients)
now the problem is that at the event how do we pass the client's ID to
compare with the list of clients' ID ?
if i do the simple way u told me, it will actually replaces the previous
client's ID with new client's ID..
-
Re: how to pass a variable to a timer's handler?
This timer approach sounds like a bizzare (and worse still, resource hungry)
way of doing things.
Why not simply use a HashTable keyed on the name that the client has
clicked, then store in the HashTable an object that holds the time that the
request was sent.
When a response comes in, you simply delete the associated entry from the
hash table.
You can detect multiple clicks by simply looking at the HashTable and seeing
if that key already exists, in which case you ignore the new click.
You can then periodically scan the HashTable and check for requests that are
older than the timeout you require, and then delete them or display an
error.
Cheers,
Jason (iGadget)
"brandon" <brandonheng@hotmail.com> wrote in message
news:3dba954c@tnews.web.devx.com...
> well, let me tell you my scenario to make understandings easier..
> i am building a client/server system.. when client[s] connects to the
> server, particular information will be displayed on server
>
> user(from server) may click the client's info from treeview and it will
send
> a request to client.. asking for the updated info.. then update the
central
> database..
>
> problem is we dun wan the user( from server) to send the request many
times,
> when the user starts clicking.. right? so, i set a timer on the treeview
> afterselect..
> since there will be many clients connected to the server.. it will
> inappropriate to use boolean variables to access the sending request
code..
> instead.. i tried to use strings where if i instr the variable for the pc
> name.. if dun have then it'll send the request and add the client's ID to
> the list.. if have it will skip..
>
> at the ontimer event.. it will get the list of clients already sent the
> request.. and get the string on which client called it.. it will remove
the
> client's ID from the list of clients' ID..
>
> the string(stringManyClients) containing the list of clients' ID can be
> delclared as public.. we need to declare a string(MyString) for the timer
to
> compare the list of clients(stringManyClients)
>
>
> now the problem is that at the event how do we pass the client's ID to
> compare with the list of clients' ID ?
>
> if i do the simple way u told me, it will actually replaces the previous
> client's ID with new client's ID..
>
>
>
>
>
>
>
-
Re: how to pass a variable to a timer's handler?
hey, good idea man..
but i do not know how to use the hashtable.. never did one b4.
can you PLS show me how to do it? provide a sample perhaps?
thank you for your idea and hopefully your help.
regards,
Brandon
"Jason Sobell (iGadget)" <iGadget_@hotmail.com> wrote in message
news:3dba978a@tnews.web.devx.com...
> This timer approach sounds like a bizzare (and worse still, resource
hungry)
> way of doing things.
> Why not simply use a HashTable keyed on the name that the client has
> clicked, then store in the HashTable an object that holds the time that
the
> request was sent.
> When a response comes in, you simply delete the associated entry from the
> hash table.
> You can detect multiple clicks by simply looking at the HashTable and
seeing
> if that key already exists, in which case you ignore the new click.
> You can then periodically scan the HashTable and check for requests that
are
> older than the timeout you require, and then delete them or display an
> error.
>
> Cheers,
> Jason (iGadget)
-
Re: how to pass a variable to a timer's handler?
Just look up "Hashtable Class" Filtered by "Visual Basic and Related" in the
..Net help files. There is a full coded example of how to use them.
Cheers,
Jason
"brandon" <brandonheng@hotmail.com> wrote in message
news:3dbaa4b7@tnews.web.devx.com...
>
> hey, good idea man..
> but i do not know how to use the hashtable.. never did one b4.
>
> can you PLS show me how to do it? provide a sample perhaps?
>
> thank you for your idea and hopefully your help.
>
> regards,
> Brandon
> "Jason Sobell (iGadget)" <iGadget_@hotmail.com> wrote in message
> news:3dba978a@tnews.web.devx.com...
> > This timer approach sounds like a bizzare (and worse still, resource
> hungry)
> > way of doing things.
> > Why not simply use a HashTable keyed on the name that the client has
> > clicked, then store in the HashTable an object that holds the time that
> the
> > request was sent.
> > When a response comes in, you simply delete the associated entry from
the
> > hash table.
> > You can detect multiple clicks by simply looking at the HashTable and
> seeing
> > if that key already exists, in which case you ignore the new click.
> > You can then periodically scan the HashTable and check for requests that
> are
> > older than the timeout you require, and then delete them or display an
> > error.
> >
> > Cheers,
> > Jason (iGadget)
>
>
>
-
Re: how to pass a variable to a timer's handler?
these codes look wonderful.. just that it gives out error
C:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\WindowsApplication11\Form1.vb(89): Method 'Public Sub
OnTimer(source As Object, e As System.EventArgs)' does not have the same
signature as delegate 'Delegate Sub ElapsedEventHandler(sender As Object, e
As System.Timers.ElapsedEventArgs)'.
whats wrong with it?
"iGadget" <iGadget_@hotmail.com> wrote in message
news:3dba8b26@tnews.web.devx.com...
> There are two ways that spring to mind:
>
> 1. If you only have one timer running at any time, make 'cut' a global
> variable as you have with 'msg'. Messy, but would work.
>
> 2. (and this is neat, really showing what you can do with .Net!)
> Create a class to handle the event, setting the variables to any values
you
> need (like your 'cut' variable)
> Point your timer to this instance of the class.
> Here is a sample I wrote:
>
> Public Class Form1
> Inherits System.Windows.Forms.Form
> Delegate Sub OnTimerHandler(ByVal source As Object, ByVal e As
> EventArgs)
>
> #Region " Windows Form Designer generated code "
> 'Create a form with a Button
> #End Region
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> AddHandler Timer1.Tick, AddressOf New TimerHandler("First
> timer!").OnTimer
> Timer1.Enabled = True
> End Sub
>
> End Class
>
> Public Class TimerHandler
> Private StringToShow As String
> Sub New(ByVal pStringToShow As String)
> StringToShow = pStringToShow
> End Sub
>
> Public Sub OnTimer(ByVal source As Object, ByVal e As EventArgs)
> CType(source, Windows.Forms.Timer).Enabled = False
> MsgBox("Got timer: " + StringToShow)
> End Sub
> End Class
>
> The beauty of this is that you can create as many timers as you like and
> have each point to its own class to do whatever you like.
> The line AddHandler Timer1.Tick, AddressOf New TimerHandler("First
> timer!").OnTimer shows how easy this is to do.
>
> If you want to be _really_ fancy, here is some code to create dynamic
timers
> and handlers, triggering events, and then removing the event handlers
> automatically:
>
>
> Public Class Form1
> Inherits System.Windows.Forms.Form
> #Region " Windows Form Designer generated code "
> 'Create a form with a Button
> #End Region
>
> Delegate Sub OnTimerHandler(ByVal source As Object, ByVal e As
> System.Timers.ElapsedEventArgs)
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> AddHandler New MyTimer(1000).Elapsed, AddressOf New TimerHandler("First
> timer after 1000!").OnTimer
> AddHandler New MyTimer(2000).Elapsed, AddressOf New TimerHandler("First
> timer after 2000!").OnTimer
> AddHandler New MyTimer(2500).Elapsed, AddressOf New TimerHandler("First
> timer after 2500!").OnTimer
> AddHandler New MyTimer(3000).Elapsed, AddressOf New TimerHandler("First
> timer after 3000!").OnTimer
> End Sub
> End Class
>
> Public Class TimerHandler
> Private StringToShow As String
> Sub New(ByVal pStringToShow As String)
> StringToShow = pStringToShow
> End Sub
>
> Public Sub OnTimer(ByVal source As Object, ByVal e As
> System.Timers.ElapsedEventArgs)
> CType(source, System.Timers.Timer).Enabled = False
> RemoveHandler CType(source, MyTimer).Elapsed, AddressOf Me.OnTimer
> MsgBox("Got timer: " + StringToShow)
> End Sub
>
> End Class
>
> Public Class MyTimer : Inherits System.Timers.Timer
> Sub New(ByVal pInterval As Integer)
> Me.Interval = pInterval
> Me.Enabled = True
> End Sub
> End Class
>
> Hmm.. This looks so useful that perhaps I should stick this up on the web
> somewhere.
> Hope this helps you a bit.
>
> Cheers,
> iGadget
>
-
Re: how to pass a variable to a timer's handler?
i got it working now..
but what does the line below do?
Delegate Sub OnTimerHandler(ByVal source As Object, ByVal e As EventArgs)
i deleted the line, and the code runs the same..
-
Re: how to pass a variable to a timer's handler?
"Jason Sobell (iGadget)" <iGadget_@hotmail.com> wrote
> This timer approach sounds like a bizzare (and worse still, resource hungry)
> way of doing things.
I agree.
> Why not simply use a HashTable keyed on the name that the client has
> clicked, then store in the HashTable an object that holds the time that the
> request was sent.
> When a response comes in, you simply delete the associated entry from the
> hash table.
But if response times are in the millisecond range, that would not stop repeated
requests. What if, for example, it was desired to restrict requests to one every
10 seconds to keep the DB from being updated so often?
> >
> > user(from server) may click the client's info from treeview and it will send
> > a request to client.. asking for the updated info.. then update the central
> > database..
> >
> > problem is we dun wan the user( from server) to send the request many times,
I was going to suggest using a queue that contains objects that represent the client
info and the time the request was made. At each click the queue could be cleared
of any old requests (past 10 seconds old) and checked for objects that match the
client info. If no match is found a new object is added to the queue and the request
sent.
LFS
-
Re: how to pass a variable to a timer's handler?
thank you..
thats what i just did
cheers,
Brandon
"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:3dbb1384@tnews.web.devx.com...
> "Jason Sobell (iGadget)" <iGadget_@hotmail.com> wrote
> > This timer approach sounds like a bizzare (and worse still, resource
hungry)
> > way of doing things.
>
> I agree.
>
> > Why not simply use a HashTable keyed on the name that the client has
> > clicked, then store in the HashTable an object that holds the time that
the
> > request was sent.
> > When a response comes in, you simply delete the associated entry from
the
> > hash table.
>
> But if response times are in the millisecond range, that would not stop
repeated
> requests. What if, for example, it was desired to restrict requests to
one every
> 10 seconds to keep the DB from being updated so often?
>
> > >
> > > user(from server) may click the client's info from treeview and it
will send
> > > a request to client.. asking for the updated info.. then update the
central
> > > database..
> > >
> > > problem is we dun wan the user( from server) to send the request many
times,
>
> I was going to suggest using a queue that contains objects that represent
the client
> info and the time the request was made. At each click the queue could be
cleared
> of any old requests (past 10 seconds old) and checked for objects that
match the
> client info. If no match is found a new object is added to the queue and
the request
> sent.
>
> LFS
>
>
>
-
Re: how to pass a variable to a timer's handler?
Look up 'delegates' in the help pages. It is basically a way of specifying
a template for callback functions.
I'm not sure if there is some sort of inconsistency of bug in VS.Net,
because you shouldn't be able to delete that line, and I had the same
problem as you with the "does not have the same signature as delegate" error
when I was writing it. I suspect that the Delegate definition line is not
always being freshly compiled.
Cheers,
Jason
"brandon" <brandonheng@hotmail.com> wrote in message
news:3dbabf88@tnews.web.devx.com...
> i got it working now..
> but what does the line below do?
> Delegate Sub OnTimerHandler(ByVal source As Object, ByVal e As EventArgs)
>
> i deleted the line, and the code runs the same..
>
>
-
Re: how to pass a variable to a timer's handler?
"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:3dbb1384@tnews.web.devx.com...
> "Jason Sobell (iGadget)" <iGadget_@hotmail.com> wrote
> > This timer approach sounds like a bizzare (and worse still, resource
hungry)
> > way of doing things.
>
> I agree.
>
> > Why not simply use a HashTable keyed on the name that the client has
> > clicked, then store in the HashTable an object that holds the time that
the
> > request was sent.
> > When a response comes in, you simply delete the associated entry from
the
> > hash table.
>
> But if response times are in the millisecond range, that would not stop
repeated
> requests.
? Of course it would. { Is there a request for PC#2 already in the table?
Yes - ignore this one }
> What if, for example, it was desired to restrict requests to one every
> 10 seconds to keep the DB from being updated so often?
That is exactly what he was asking for.
> > >
> > > user(from server) may click the client's info from treeview and it
will send
> > > a request to client.. asking for the updated info.. then update the
central
> > > database..
> > >
> > > problem is we dun wan the user( from server) to send the request many
times,
>
> I was going to suggest using a queue that contains objects that represent
the client
> info and the time the request was made. At each click the queue could be
cleared
> of any old requests (past 10 seconds old) and checked for objects that
match the
> client info. If no match is found a new object is added to the queue and
the request
> sent.
What you describe is similar to what I suggested, except you are suggesting
a queue instead of a hashtable.
Since the situation only calls for (and is trying to enforce) one
outstanding request at any one time, why use a queue, but more importantly,
a queue cannot be scanned to see if a request is outstanding (you only have
access to the end value) so it would not work in this scenario.
The requirement is for a data structure that allows a keyed pair (keyed on
PC name) so a HashTable is the fastest and most applicable solution.
The only time another data structure would be required would be if an
ordered list of pending requests was needed, in which case a Dictionary
based structure could be used to maintain an order too.
Cheers,
Jason
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|