我在.net 4.0中有一个简单的计时器,间隔设置为1000(= 1秒)并且enabled = 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
是我使用的。
答案 0 :(得分:1)
如果您在Stop
上致电Forms.Timer
,则只需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;
}
答案 1 :(得分:0)
我在init方法中使用Start,事件中的Stop / Start是将当前计数设置为0(重新开始),因为该事件可以被另一个回调触发。
这可能是你的问题。如果您使用Start
/ Stop
,请勿共享计时器。您可能应该使用队列,而队列又由BackgroundThread
处理。