473,386 Members | 1,795 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,386 software developers and data experts.

Raise Timer_Elapsed event

Hello,
is it possible to manually raise the event Elapsed of the timer object ?

Vittorio
Nov 20 '05 #1
12 2912
* "Vittorio Pavesi" <n@spam.net> scripsit:
is it possible to manually raise the event Elapsed of the timer object ?


Place the code of the event handler in a separate method, then call this
method from within the event handler and any other place ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
Thank you for the workaround, this means that is not possible to raise that
kind of event ?

Vittorio

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> ha scritto nel
messaggio news:2k************@uni-berlin.de...
* "Vittorio Pavesi" <n@spam.net> scripsit:
is it possible to manually raise the event Elapsed of the timer object ?


Place the code of the event handler in a separate method, then call this
method from within the event handler and any other place ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #3
Hi Vittorio,

You can not raise the event however call the method, as with every event.

As parameters you can normaly always pass (Nothing, Nothing)

I hope this helps,

Cor
Nov 20 '05 #4
* "Vittorio Pavesi" <n@spam.net> scripsit:
Thank you for the workaround, this means that is not possible to raise that
kind of event ?


It simply doesn't make much sense IMO.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
* "Cor Ligthert" <no**********@planet.nl> scripsit:
You can not raise the event however call the method, as with every event.

As parameters you can normaly always pass (Nothing, Nothing)


This would call /one/ event handler, sure. Nevertheless, I don't think
that's a good idea, and placing the code of the handler in a separate
method would be the better approach.

If it's necessary to raise the event in certain situations, you can try
to inherit from the timer class and call the base class's
'On<EventName>' method to raise the event.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #6
Really thanks for the explanation !

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> ha scritto nel
messaggio news:2k************@uni-berlin.de...
* "Vittorio Pavesi" <n@spam.net> scripsit:
Thank you for the workaround, this means that is not possible to raise that kind of event ?


It simply doesn't make much sense IMO.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #7
Hi Herfried,

This time I do *not* disagree with you, however it is possible.
This would mean that the first action would be to disable the timer.
However it was to show that you could do this with almost every event.

(Although for that handler method it would be probably the same, I think it
is not right doing this for a timer and certainly not for another timer than
the forms.timer, however just my thought).

:-)

Cor
Nov 20 '05 #8
* "Vittorio Pavesi" <n@spam.net> scripsit:
Really thanks for the explanation !


In other words: No, you cannot /raise/ an event from outside a class
except the class provides a method that raises the event internally. I
doubt that this is the case in the case of the timer.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #9
* "Cor Ligthert" <no**********@planet.nl> scripsit:
This time I do *not* disagree with you, however it is possible.
This would mean that the first action would be to disable the timer.
However it was to show that you could do this with almost every event.

(Although for that handler method it would be probably the same, I think it
is not right doing this for a timer and certainly not for another timer than
the forms.timer, however just my thought).


You /can/ call an event handler the way you showed, but I would not pass
'Nothing' to the parameters. Instead I would pass a reference to the
control the event is belonging to in the 'sender' parameter and
'EventArgs.Empty' in the 2nd parameter.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10
Hi Herfried,

I find the fact not to do that for a timer more important than passing a
reference.

For the passing of parameters completely confirm the signature I see no
sense when it is not conform the sense of that signature although it are
objects.

So passing the same parameters when I have them is something I would always
do, however giving false parameters is in my eyes even more confusing, than
is nothing even better because that is testable to nothing.

To give you an example, you know that I like it to make a collection of
controls and than to add handlers to it, when I not send always the standard
object I would have to do in those events.

If typeoff sender Is Button then
if directcast(sender, button).name = etc.

This can as well as
If Not sender is Nothing then etc.

So there is not much benefit in my opinion when the sender is not the
expected object.

This is a long philosophic message this time.

Cor
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:2k************@uni-berlin.de...
* "Cor Ligthert" <no**********@planet.nl> scripsit:
This time I do *not* disagree with you, however it is possible.
This would mean that the first action would be to disable the timer.
However it was to show that you could do this with almost every event.

(Although for that handler method it would be probably the same, I think it is not right doing this for a timer and certainly not for another timer than the forms.timer, however just my thought).


You /can/ call an event handler the way you showed, but I would not pass
'Nothing' to the parameters. Instead I would pass a reference to the
control the event is belonging to in the 'sender' parameter and
'EventArgs.Empty' in the 2nd parameter.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #11
Vittorio,
In addition to the other comments:

Which Timer object (.NET has 3 timer objects)?

It appears that only the System.Timers.Timer object has an Elapsed event, as
Herfried stated, this Timer does not allow others to raise its events. The
normal patterns is to have an overridable protected OnElapsed method that
derived objects can call to raise the event.

In addition to considering the other suggestions, I would consider creating
a new class that encapsulates the Timer, this new class would have its own
Elapsed event, this new class would handle the Timer.Elapsed event, raising
its own Elapsed event, plus it would have a public method allowing other
objects to raise its Elapsed event. I would only really consider creating
this new class, if I wanted or needed to have multiple handlers of a single
Timer's Elapsed event, making calling the handler or a common procedure
"messy".

Hope this helps
Jay

"Vittorio Pavesi" <n@spam.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello,
is it possible to manually raise the event Elapsed of the timer object ?

Vittorio

Nov 20 '05 #12
* "Cor Ligthert" <no**********@planet.nl> scripsit:
I find the fact not to do that for a timer more important than passing a
reference.
The event has basically nothing to do with the timer. It's sort of
"callback" that is called when the event is raised. Passing empty data
can cause a 'NullReferenceException' if you access 'sender' and 'e'
inside the event handler. If you are the author of the code, that's not
a problem, but let's think about a team of programmers and one
programmer adds code to the event handler that accesses 'sender' and 'e'
inside the event handler and *BANG*, an exception is thrown.

There is a "pattern" in .NET's event handling, so that the developer can
expect 'sender' and 'e' to be supplied.
So passing the same parameters when I have them is something I would always
do, however giving false parameters is in my eyes even more confusing, than
is nothing even better because that is testable to nothing.
Why giving false parameters? By passing the reference to the timer in
the 1st parameter you are passing the correct value. By passing an
appropriate 'EventArgs' object (or an instance of one of its subclasses,
depending on the case) is not passing a wrong value too.
To give you an example, you know that I like it to make a collection of
controls and than to add handlers to it, when I not send always the standard
object I would have to do in those events.

If typeoff sender Is Button then
if directcast(sender, button).name = etc.

This can as well as
If Not sender is Nothing then etc.

So there is not much benefit in my opinion when the sender is not the
expected object.


Sure, that's why I would pass the expected object.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #13

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

Similar topics

1
by: Dan Cimpoiesu | last post by:
I have a remoting object, derived from MarshalByRefComponent, that I instantiate on the client side, with Activator.GetObject. Can I receive events fired on the server, on the client? How?
2
by: IcedCrow | last post by:
Subject says it all. I want to raise an event in Sub New of a class but it is not being raised to my client app. I can raise events just fine in other procedures... just not sub new. Why is...
4
by: Jeremy | last post by:
I have a combobox with a SelectionChangeCommitted event handler, and am having a problem raising this event. For example, raiseEvent mycombobox.SelectionChangeCommitted gives me an error...
8
by: NewUser | last post by:
Hello, I'm a new user to Visual Basic.net and I would appreciate any help regarding a problem I have. I have searched the posts in this newsgroup and the VB library for threading topics, but I...
2
by: Pietro | last post by:
Hello, somebody know how to raise an event from a nested class? I have two classes, the class1 with 1 events, and a nested class (class2) inside the class1. So... How can I raise class1 events...
1
by: Anonieko | last post by:
I know Visual Studio lacked support on easily writing code to raise events from a ascx user control ( because you have to hand write them)....
3
by: =?Utf-8?B?Ulc=?= | last post by:
I constructed a new Class with some private members. I would like an event to be raised on the moment the value of one of those private members is changed. How do I define an event for that...
5
by: Mike | last post by:
Hi group; Let say I have an object called Account, that raises an event called AccountLow with its owns EventArgs, and when this event gets raised, I will like to raise another custom...
2
by: Sin Jeong-hun | last post by:
Suppose class Engine do something in another thread and raise events. class Engine { Thread Worker; public event ... EngineMessage; public void Start() { Worker=new Thread(new...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...

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.