The timer is believed to be widely used. It is convenient for you to perform certain operations on a regular basis. However, after the operation is completed, you need to stop the timer. The landlord made such a low-level mistake. The following code (the WPF project is used as an example ):
Public partial class mainwindow: window {private dispatchertimer checkusedtimer = NULL; Public mainwindow () {initializecomponent (); int timeinterval = 10; checkusedtimer = new dispatchertimer (); checkusedtimer. interval = timespan. fromseconds (timeinterval); checkusedtimer. tick + = new eventhandler (checkusedtimer_tick); checkusedtimer. start ();} internal void checkusedtimer_tick (Object sender, even Targs e) {checkusedtimer = NULL; MessageBox. Show ("the timer is running! ");}}
Note: checkusedtimer = NULL. In this example, it is set to null. It is time to stop working. The result shows that checkusedtimer_tick is still executing, regardless of whether checkusedtimer is already null. So we still need to honestly use its own stop () method.
As follows:
Public partial class mainwindow: window {private dispatchertimer checkusedtimer = NULL; Public mainwindow () {initializecomponent (); int timeinterval = 10; checkusedtimer = new dispatchertimer (); checkusedtimer. interval = timespan. fromseconds (timeinterval); checkusedtimer. tick + = new eventhandler (checkusedtimer_tick); checkusedtimer. start ();} internal void checkusedtimer_tick (Object sender, even Targs e) {// The if (checkusedtimer! = NULL) {checkusedtimer. Stop () ;}// if it is set to null, the timer after startup is still working checkusedtimer = NULL; MessageBox. Show ("the timer is running! ");}}
Download this article: http://download.csdn.net/detail/yysyangyangyangshan/5330101