作为系统“自检”应用程序的一部分,我有一个应用程序每 60 秒调用一次 DLL 中的静态方法。当我手动运行这些方法时,它们都在不到 10 秒的时间内完成。我的问题是 timer.elapsed 事件触发了两次,一个接一个。除此之外,每经过一次计时器,事件就会再触发一次。(例如,第一次是 2 次触发,第二次是 3 次,第三次是 4 次等)我尝试设置 timer.AutoReset = false 以及设置 timer.Enabled = false 在经过的事件开始时,然后将其设置为 true在活动结束时。我试过重置事件的间隔。我发现的每个帖子都表明上述操作应该已经解决了这个问题。谁能帮我找到我缺少的东西?
static Timer cycle = new Timer();
static int cycCount = 0;
static void Main(string[] args)
{
Console.WriteLine("Firebird Survivor Auto Cycle Started.");
Console.CancelKeyPress += Console_CancelKeyPress;
cycle.Interval = 60000; //set interval for service checks.
cycle.Elapsed += new ElapsedEventHandler(CycleComplete_Elapsed);
cycle.AutoReset = false;
cycle.Enabled = true;
cycle.Elapsed += CycleComplete_Elapsed;
while (1 == 1) //stop main method from completing indefinitely
{
//WAIT FOR TIMER TO ELAPSE
}
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
cycle = null;
}
static void CycleComplete_Elapsed(object sender, ElapsedEventArgs e) //method triggered by timer
{
cycle.Enabled = false;
cycCount++;
WormholeServiceControls.CheckWormHoleStatus();
TimeControls.CheckTimePl(); //call time check
PegasusServiceControls.CheckPegasusStatus(null);
Console.Clear();
Console.WriteLine("--------------------------");
Console.WriteLine(String.Format("| Successful Cycles: {0} |", cycCount));
Console.WriteLine("--------------------------");
cycle.Enabled = true;
}