嗨我已经创建了一个Windows服务,并且在那里有两个不同的计时器变量。第一个每5秒发射一次,然后做它需要做的事情。第二个每30秒发射一次。在同一代码中以不同的时间间隔触发两个不同的计时器变量问题
我确保当函数得到执行时,禁用两个定时器,并在函数完成后启用。
我的问题是我的第二个更长的时间(30秒)计时器从不会触发第一个射击。我在3个月前创建了这项服务,到现在为止工作情况良好。但是现在我没有看到定时器在我创建的一个简单的Windows应用程序中正常工作以测试其工作。
timerLonger= New System.Timers.Timer()
timerLonger.Interval = 30000
timerLonger.Enabled = True
timerShorter= New System.Timers.Timer()
timerShorter.Interval = 5000
timerShorter.Enabled = True
AddHandler timerLonger.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Process1)
AddHandler timerShorter.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Process2)
Sub Process1()
timerLonger.Enabled = False
timerShorter.Enabled = False
DoProcessing1()
timerShorter.Enabled = True
timerLonger.Enabled = True
End Sub
Sub Process2()
timerLonger.Enabled = False
timerShorter.Enabled = False
DoProcessing2()
timerShorter.Enabled = True
timerLonger.Enabled = True
End Sub
可能在添加处理程序后设置'Enabled'?另一方面,更可能的原因可能是线程问题。请参阅[文档](http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx)。我认为你应该每个线程只使用一个计时器。 – 2011-03-31 17:36:11