I have a timer as defined below. The timer executes a long running task. The problem i have is when the timer is running the interval has elapsed again and another timer execution starts i.e _timer_Elapsed
. How can i get the timer to execute one interval after the timer has finished. The way it is now there could be multiple timer executions happening at once and that causes all sorts of issues with my code.
protected override void OnStart(string[] args)
{
_timer = new System.Timers.Timer();
_timer.AutoReset = false;
_timer.Interval = (Convert.ToInt32(ConfigurationManager.AppSettings["CheckInterval"]));
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
_timer.Enabled = true;
_timer.Start(); // Start timer
}
public static void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
_timer.Interval = (Convert.ToInt32(ConfigurationManager.AppSettings["CheckInterval"]));
BLLGenericResult SystemCheckItems_Result = ServiceItemOperations.CheckItems(); // Long Running Task
}
catch (Exception ex)
{
// Exception handling...
}
finally
{
_timer.Start();
}
}