timer's stop()的问题,疑惑中。。。。
there is such situation when we dispose the class that contains a timer.
we stop the timer in another thread while the program is working with the timer 's elapse event and maybe timer 's elapse event will continue although timer is stopped,then maybe at this time the timer 's elapse event visit the variable that is not available because we disposed it already and there is another question if the time timer 's elapse event spend is more than timer 's interval , the elapse event will reentrant.
is what i think correct???do i need wait for the timer 's elapse event over when i dispose the class???
我翻译下:当我们在一个计时器的elapse event 中执行时间很长的操作,是否会发生重入??(if the time timer 's elapse event spend is more than timer 's interval , the elapse event will reentrant)
当我们在一个计时器的elapse event 时间中调用另外一计时器的stop()方法,那另外一计时器的elapse event 事件中的代码还会被激发吧,如果elapse event 事件中的代码正在执行,他会被停止吗,还是会继续,而且继续被激发???
如果是的话,我们需要对elapse event 中的代码进行保护???
------解决方案--------------------要看定时器是什么类型,如果是窗体定时器,则始终使用一个线程执行,当这个定时器的Interval为1s,而在其Tick事件中Sleep了2s时,定时器事件会等待,变成2s后执行。如果是系统定时器,则会开启一个新的线程执行,也就是说,到了Interval设置的时间后,一定会执行。如果在一个定时器中Stop另一个,另一个定时器会停止,如果event已经开始,则会执行完这一次的event,下次不再执行。如果elapse event中访问了非线程安全的资源,需要进行保护,其实就是多线程保护。