我是 C# 新手,我需要为一个用于监视硬件的小型应用程序使用计时器。我找到了一些计时器的参考代码,但它使用了 DoEvents()。因为,我运行计时器很长时间,有时是几天和几小时,我开始出现堆栈溢出。我现在明白是 DoEvents() 导致了这种情况,这是大多数人推荐使用的。您建议我使用什么功能代替 DoEvents 来设置我的计时器?
我的代码:
private void BeginMonitoringClick() {
{
myTimer.Tick += new EventHandler(TimerEventProcessor); // myTimer declared elsewhere
myTimer.Interval = 2000;
myTimer.Start();
while(!exitFlag)
{
Application.DoEvents();
}
}
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs){
// Talk to hardware, see if it is doing OK
}