开发者

Disposing method which returns Timer

I have a method which returns System.Timers.Timer and then I use it along the code. But MS开发者_Go百科 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 implements IDisposable so just put it inside using:

using(var timer = MyMethodThatReturnsTimer())
{
}

If you cannot use using than just call Dispose yourself.


Since System.Timers.Timer impliments IDisposable you can wrap your method call in a using statement to make sure Dispose is called.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜