473,386 Members | 1,698 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.

Can a Timer destroy itself?

I'd like to be able to destroy a Timer in it's own event handler. That is,
within it's tick handler I'd like to delete the Timer itself (e.g., for
one-shot timers). Is this possible?

In general, can a class instance destroy itself via one of it's own methods?
Nov 17 '05 #1
4 1778
Peter Oliphant wrote:
In general, can a class instance destroy itself via one of it's own methods?


Firing an event is like calling a method or function. So the event
handler is called from the object itself, which is still alive at that
time, and therefore it is potentially dangerous to delete it. It's OK to
delete if you are sure that the method where the event is fired from no
longer refers to any of the member variables (or internal resources).

But wait a minute, is it possible at all in .NET to delete an instance?
There is no such thing as a destructor in CLI. You can't force the
deletion of allocated objects, and the garbage collector will only
delete the object once it's not reference by anywhere else. As long as
the object is still alive, it's not going to be garbage collected.

When you Dispose an object, you only destroy its underlying handle. For
example, if it's a file object, Dipose will close the file. If it's a
Graphics object, the device context is released. But the object itself
is still alive after calling Dispose, as far as its member variables go.
Its underlying resource or unmanaged implementation is dead, though,
after a Dispose. In the new C++/CLI syntax you no longer call Dispose,
but use the delete operator instead. The delete operator performs just a
call to Dipose, if I understand it correctly. It's still the garbage
collector that removes the object from the memory.

Whether it's safe to call "delete timer1" from timer1's event handler, I
can't tell for sure, but it might be. It depends how safe its internal
implementation is.

Here's an example where it's not safe to call delete from an event handler:

ref class Unsafe
{
public:
Unsafe()
: unmanaged(new Unmanaged)
{
}
~Unsafe()
{
delete unmanaged;
}
delegate void NotifyEvent(Unsafe^ sender);
event NotifyEvent OnNotify;
void FireNotify()
{
OnNotify(this);
unmanaged->DoSomething(); // booooom!!!!!!
}
private:
Unmanaged* unmanaged;
};

If you call "delete sender" from OnNotify's handler, FireNotify will
crash, because parts of the class's internal implementation is already
dead, and the class doesn't even have protection built in against that.

Also note that an event can have multiple handlers. If you do have
multiple handlers, it can't be guaranteed that a specific handler is the
last one to be called. If you dispose the sender from the handler, other
handlers may still want to access the object. Once again, it's sometimes
possible to make such a class that's safe to be disposed from a handler,
but generally speaking such safety can not always be guaranteed. Without
knowing the implementation, you can never be sure, in my opinion.

Tom
Nov 17 '05 #2
Peter Oliphant wrote:
I'd like to be able to destroy a Timer in it's own event handler. That is,
within it's tick handler I'd like to delete the Timer itself (e.g., for
one-shot timers). Is this possible?


How about disabling it?
timer1->Enabled = false;

Or if you want delayed destruction, ::PostMessage, I'm not sure its
WinForms equivalent.

Tom
Nov 17 '05 #3
I was thinking about the ability to create Timers on the fly in such a way
that if my program goes to the same code the old timers will be gone (think
of it as trying not to have 'timer leak' much like a 'memory leak'). If the
Timers go out of scope this should do exactly that, so that is how I'll do
it.

I don't NEED this functionality really, I was just trying to give a timer
class I'm creating (more of a rapper on the existing Timer class) as much
functionality as possible. But no need to do this if it's too dangerous! :)

Thanks for the detailed response!!! :)

"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:uz****************@TK2MSFTNGP10.phx.gbl...
Peter Oliphant wrote:
I'd like to be able to destroy a Timer in it's own event handler. That
is, within it's tick handler I'd like to delete the Timer itself (e.g.,
for one-shot timers). Is this possible?


How about disabling it?
timer1->Enabled = false;

Or if you want delayed destruction, ::PostMessage, I'm not sure its
WinForms equivalent.

Tom

Nov 17 '05 #4
Peter Oliphant wrote:
I was thinking about the ability to create Timers on the fly in such a way
that if my program goes to the same code the old timers will be gone (think
of it as trying not to have 'timer leak' much like a 'memory leak').


I wouldn't worry about that. A timer that is not enabled does not exist.
I'm not sure about the WinForms timer, but in Delphi when you disable a
timer, the framework automatically calls KillTimer. So the underlying
timer resource simply does not exist when the timer is not enabled. What
exists is a few bytes of memory (required by the timer wrapper object)
on the heap, that's it. This also means that you could pre-create a
timer for every situation, without having to worry about exceeding the
maximum system timer count. I have no reason to believe the WinForms
implementation consumes a timer reasource when a timer is disabled. It
would be a bad implementation in my opinion.

Tom
Nov 17 '05 #5

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

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
11
by: Steve Jorgensen | last post by:
I've recently been playing with some UI ideas that require the user of a timer to drive animation. The problem I'm having is that Access routinely stops firing timer events for long periods of...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
2
by: Alvo von Cossel I | last post by:
hi, i have 2 forms. when the application runs, 1 of them should run for 300 milliseconds and then closes. after this, the other form should open. this works but the second form closes after...
6
by: whtinkm | last post by:
Hi, All Recently, my project need some code like following: using System; using System.Threading; namespace MyTimerTest { class Class1 {
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...
5
by: Flack | last post by:
Hey guys, Here is what I am trying to achieve: I have a grid, and every once in a while the grid will receive a message to add a new row and highlight it (change the backcolor) for five...
4
by: Dmitri Sologoubenko | last post by:
Hi, guys! I need a C++ class (Linux/POSIX/GNU C++), which can be started and stopped, and calls a virtual callback instance method (e.g. "expired()") when a given time has elapsed. High...
4
by: Curious | last post by:
Hi, I have a timer object that's launched as below: mTimer = new System.Threading.Timer (new TimerCallBack(SubscribeTrade), null, 15000, 15000); void SubscribeTrade (object state) {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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.