I'm building my project with the "Microsoft Minimal Rules" code analysis set and it gives me CA2000 on this method:
private Timer InitializeTimer(double intervalInSeconds)
{
Timer timer = null;
try
{
timer = new Timer { Interval = intervalInSeconds * 1000, Enabled = true };
timer.Elapsed += timer_Elapsed;
timer.Start();
}
catch
{
if (timer != null)
{
timer.Dispose();
}
}
return timer;
}
This method just creates a new System.Timers.Timer
from an interval in seconds. I have three such timers running (one for every second, one every minute and one every half hour). Maybe it's better to have one timer and check in the elapsed event handler whether a minute or a half hour has passed, but I don't know, this is easier at this time, it's inherited code and I don't want to break everything yet.
This method gives me the infamous
Warning 21 CA2000 : Microsoft.Reliability : In method 'TimerManager.InitializeTimer(double)', call System.IDisposable.Dispose on object '<>g__initLocal0' before all references to it are out of scope.
Now I AM calling Dispose in the catch and thought this would be enough? I'm also disposing all timers in the class' own IDisposable implementation.
What am I missing here?