In my project i created System.Timers.Timer
object and the interval is set to 10 min.
For every 10 min I am getting elapsed event. In this event handler i am executing some code.
Before executing this code I am setting Enabled
property equal to false
because if the handler takes longer to execute than the next interval another thread executes the elapsed event.
Problem here is suddenly Elapsed
event is stopped.
I have read some articles and suspecting that the moment enabled property set to false garbagecollector frees the timer object.
If it is right please tell me the solution.
Below is example code:
public class Timer1
{
private static System.Timers.Timer aTimer;
public static void Main()
{
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 10min.
aTimer.Interval = 600000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
aTimer.Enabled = false;
// excutes some code
aTimer.Enabled = true;
}
}