I am using System.Timers.Timer Class and I want to change it's Interval in the elapsed function. When i set Elapsed property to different value, somehow elapsed function starts firing and firing again althrough timer's Autoreset property is set to false.
my code:
var timer = new Timer()
{
Interval = 1000,
AutoReset = false,
};
timer.Enabled = true;
timer.Elapsed += (sender, eventArgs) =>
{
Console.WriteLine("Timer fires");
timer.Interval = 2000;
};
The code results in firing timer again and again when i just wanted to change interval for the latter use of the timer.
It would be nice to know why this happens and what should i do to achieve desired behaviour.
Timer.Close will release all resources attached with this timer object and it seems to help here.
In the Elapsed event handler logic, when the Timer.Interval is set to a number greater than zero, it's telling to invoke the "Elapsed" event again and the whole logic goes into a infinite loop.