>我在 .net 4.0 中有一个简单的计时器,间隔设置为 1000(= 1 秒)并启用 = true。要启动计时器,我使用.Start()
.到目前为止一切正常。
然后在计时器的情况下,我得到了这个:
private void MyTimerEvent()
{
myTimer.Stop();
myTimer.Start();
//Some other work is done not related to the timer
}
问题是,一旦计时器事件运行,它就会停止抛出该事件,并且即使在myTimer.Start()之后,它似乎也设置为enabled = false
;有时也是如此。但我根本没有设定enabled = false
?
我做错了什么?
编辑:System.Windows.Forms.Timer
是我使用的那个。
如果你在Forms.Timer
上调用Stop
,它只会执行一个Enabled=False
。与Start
相同。如果您的计时器enabled
则无需调用Start
因为计时器已在运行。您可以使用Reflector等人检查自己:
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Start()
{
this.Enabled = true;
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Stop()
{
this.Enabled = false;
}
我在 init 方法中使用 Start at,事件中的停止/启动是将当前计数设置为 0(重新开始),因为事件可以由另一个回调触发。
那可能是你的问题。如果您使用的是 Start
/Stop
,请不要共享计时器。您可能应该改用队列,而队列又由BackgroundThread
处理。