Timer stop in C #

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.