title: Auto-dispose Threading.Timer
body: <p>I want something very simple</p>
  <pre><code>// increment counter
  Interlocked.Increment(ref _counter);
  
  // automatically decrement counter after 1 sec
  Timer timer = new Timer((o) =&gt; {
      Interlocked.Decrement(ref _counter);
      (o as Timer).Dispose();
  }, timer, 1000, Timeout.Infinite);
  </code></pre>
  <p>this code, however, is not compile-able</p>
  <blockquote>
  <p>Use of unassigned local variable 'timer'</p>
  </blockquote>
  <p>Any easy way to fix that? It must be <code>Threading.Timer</code>.</p>
  <p>P.S.: I am not sure if I <strong>must</strong> call <code>Dispose</code>, it's obviously un-managed resource and it's <code>IDisposable</code>, still on <a href="http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx" rel="noreferrer">msdn</a> they warn</p>
  <blockquote>
  <p>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.</p>
  </blockquote>
  <p>And I actually want it to be be collected (auto-disposed?). So, to dispose or to not dispose?</p>