我想要一些非常简单的东西
// 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);
但是,此代码不可编译
Use of unassigned local variable ‘timer’
有什么简单的方法来修复它吗?它必须是Threading.Timer.
P.S.:我不确定我是否必须调用Dispose,它显然是非托管资源,它是IDisposable,仍然在msdn他们警告
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.
我实际上希望它被收集(自动处理?).那么,处置还是不处置?