present code is currently structured as follows:
System.Timers.Timer myTimer;
public void FirstMethod() {
myTimer;= new System.Timers.Timer();
myTimer.start();
SecondMethod();
}
public void SecondMethod(){
//several things happen here and then
myTimer.stop();
}
I've been advised that I could use using
to correctly garbage collect the Timer object. So I've tried to apply something like the following to my code (taken from here):
using (SomeClass someClass = new SomeClass())
{
someClass.DoSomething();
}
I assume the following will error because myTimer
is not known by SecondMethod()
?
public void FirstMethod() {
using (System.Timers.Timer myTimer = new System.Timers.Timer())
{
myTimer.start();
SecondMethod();
}
}
public void SecondMethod(){
//several things happen here and then
myTimer.stop();
}