title: Can Timers get automatically garbage collected?
body: <p>When you use a <code>Timer</code> or a <code>Thread</code> that will just run for the entire lifetime of the program do you need to keep a reference to them to prevent them from being garbage collected?</p>
  
  <p>Please put aside the fact that the below program could have <code>timer</code> as a static variable in the class, this is just a toy example to show the issue.</p>
  
  <pre><code>public class Program
  {
      static void Main(string[] args)
      {
          CreateTimer();
  
          Console.ReadLine();
      }
  
      private static void CreateTimer()
      {
          var program = new Program();
  
          var timer = new Timer();
          timer.Elapsed += program.TimerElapsed;
          timer.Interval = 30000;
          timer.AutoReset = false;
          timer.Enabled = true;
      }
  
      private void TimerElapsed(object sender, ElapsedEventArgs e)
      {
          var timerCast = (Timer)sender;
  
          Console.WriteLine("Timer fired at in thread {0}", GetCurrentThreadId());
  
          timerCast.Enabled = true;
      }
  
      ~Program()
      {
          Console.WriteLine("Program Finalized");
      }
  
      [DllImport("kernel32.dll")]
      static extern uint GetCurrentThreadId();
  }
  </code></pre>
  
  <p>Could the timer get collected in that above example? I ran it for a while and I never got a exception nor a message saying <code>~Program()</code> was called.</p>
  
  <p><strong>UPDATE:</strong> I found out from <a href="https://stackoverflow.com/questions/9011831/new-thread-and-garbage-collection">this question</a> (<a href="https://stackoverflow.com/questions/18136735/can-timers-get-automatically-garbage-collected#comment26561465_18136735">thanks sethcran</a>) that threads are tracked by the CLR, but I still would like an answer about Timers.</p>