我有一个如下定义的计时器。计时器执行一个长时间运行的任务。我遇到的问题是,当计时器运行时,间隔再次过去,另一个计时器执行开始,即_timer_Elapsed
。定时器完成后如何让定时器执行一个间隔。现在的方式可能会同时发生多个计时器执行,这会导致我的代码出现各种问题。
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();
}
}