About System.Windows.Forms.Timer, Stop, and GC under heavy load.

Hi,

quote from Forms.Timer.Stop() MSDN documentation:

A timer that is disabled is subject to garbage collection.

The application code I work on happens to do things such as:


void MyTickEventHandler()
{
timer.Stop();
/*do a bit of work */
timer.Start();
}

Under heavy loads, the timer sometimes seems not to restart anymore at some point (= the timer get dead). Given the documentation, I strongly suspect (could not hook the Disposed event yet but it may help!) the GC to do some job precisely between Stop() and Start().

I'm probably going to put a GC.KeepAlive(timer) at the end of MyTickEventHandler to ensure that no garbage collection occurs on timer.

I'm just curious here, did anyone meet this situation before ?

kind regards

Thibaut

http://www.dotnetguru2.org/tbarrere

[1243 byte] By [ThibautBarrère] at [2007-12-16]
# 1
It is only subject to garbage collection if you don't hold a reference to it.

If 'timer' is a class member, then it will not available for garbage collection until it is either set to null or until nothing holds a reference to its parent class.

However, if timer is a local variable it still won't be garbage collected after timer.Stop(), as the gc realizes that it is used again when you call timer.Start().

However, saying that it is possible that the timer is being gc'd after timer.Start(), but only if it is a local variable.

Where are you using the Windows Timer? Is it within a Form?

DavidM.Kean at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...