The documentation of System.Threading.Timer
says that I should keep a live reference for it to avoid it being garbage collected. But where should I do that? My
EDIT: My original answer is rubbish. Really rubbish. I've kept it here to explain why it's rubbish though - it's in the comments, but they'd have been deleted with the answer.
GC.KeepAlive only makes sure a reference is treated as a root until after the call. In the code at the bottom of this answer, the GC.KeepAlive method would be called immediately, and then the timer would still be eligible for garbage collection. Because the newly created thread is a foreground thread, the app will run as long as it's alive (whereas the timer uses a background thread, which doesn't prevent program exit). This means that the Main method exits, but the application needs to keep running.
Arguably a simpler solution would be to run myThreadStart
in the main thread, rather than creating a new one and then letting the main thread die. In other words, a simple solution would be:
using System.Threading;
class Program {
static void Main() {
Timer timer = new Timer(myTimerCallback,
new MyStateObject(), 0, 5000);
myThreadStart();
GC.KeepAlive(timer);
}
}
I assume the real code is more complicated though - in which case using a private static variable as suggested in other answers is probably the way to go. It really will depend on the usage though. I personally prefer not to create a static field just for the sake of preventing something being collected if there's an alternative (like the above) but sometimes it's virtually the only way of doing it.
Original (bad) answer:
If you really want to allocate it in Main, then you can use GC.KeepAlive:
using System.Threading;
class Program {
static void Main() {
new Thread(myThreadStart).Start();
Timer timer = new Timer(myTimerCallback,
new MyStateObject(), 0, 5000);
GC.KeepAlive(timer);
}
}