开发者

Disposing method which returns Timer

开发者 https://www.devze.com 2023-04-04 15:35 出处:网络
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

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

精彩评论

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