views:

59

answers:

1

I've got a timer that won't trigger the associated function when the time runs out. I did set a Tick event. I set the Interval property to 12000 and I did myTimerObject.Start();.

I did however set off this timer in a seperate thread (somewhere in a BackgroundWorker). My theory is that even though the timer seems to start correctly, the thread is destroyed once the BackgroundWorker's associated DoWork function is done executing and this in turn causes the timer event to be destroyed too.

Is this true? Are there other possible reasons why the timer event doesn't occur?

+4  A: 

Pieter, the problem is not the thread from which the Timer is started, but rather the fact that you don't keep a live reference to it, hence it gets garbage collected.

From MSDN documentation on Timer (see note under "Remarks"):

As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.

Hershi
You said it so much more eloquently :-)
pst