Disposing method which returns Timer

I have a method which returns System.Timers.Timer and then I use it along the code. But MS Code Analysis gives a warning: CA2000: Dispose objects before losing scope Is it possible to manually Dispose Timer and therefore not to spot such a message?

Thank you!


由于System.Timers.Timer暗示IDisposable ,因此可以将您的方法调用包装在using语句中以确保Dispose被调用。

using(var timer = myClass.TimerMethod())
{
  // Do the work here
}

System.Timers.Timer implements IDisposable so just put it inside using :

using(var timer = MyMethodThatReturnsTimer())
{
}

If you cannot use using than just call Dispose yourself.

链接地址: http://www.djcxy.com/p/63012.html

上一篇: 友好的方式来处理物体

下一篇: 处理返回Timer的方法