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
}
精彩评论