Do Timers work when started from a BackgroundWorker?
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 do开发者_StackOverflow社区esn't occur?
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.
精彩评论