I need something very simple
// increment counter Interlocked.Increment(ref _counter); // automatically decrement counter after 1 sec Timer timer = new Timer((o) => { Interlocked.Decrement(ref _counter); (o as Timer).Dispose(); }, timer, 1000, Timeout.Infinite);
this code, however, does not compile
Using the unrecognized local variable 'timer'
Any easy way to fix this? This should be Threading.Timer .
PS: I'm not sure that I should call Dispose , this is clearly not a managed resource, and it is IDisposable , they still warn on msdn
As long as you use the timer, you must keep a link to it. As with any managed object, the timer is subject to garbage collection when there are no references to it. The fact that the timer is still active does not prevent it from gathering.
And I really want to be collected (automatically removed?). So, to dispose or not to dispose?
source share