Examples of C # timers Timer

Source: Internet
Author: User
static void Main (string[] args)        {            #region  timer            timerdemo td = New Timerdemo ("Timerdemo", +);            Td. Enabled = true;            Td. TickEvent + = Testhandler;            Thread timer = new Thread (TD. Run);            Timer. Start ();            #endregion            console.readline ();        }        <summary>        ///test events///</summary> static void Testhandler ()        {            Console.WriteLine (DateTime.Now.ToLongTimeString ());        }

  

public class Timerdemo {//thread name string _threadname;            public string ThreadName {get {return _threadname;}        Private set {_threadname = value;}        }//time interval int _timeinterval;            public int TimeInterval {get {return _timeinterval;}        set {_timeinterval = value;}        }//Current timer is enabled true: Enable false: BOOL _enabled is not enabled;            public bool Enabled {get {return _enabled;}        set {_enabled = value;}        }//events that need to be run at intervals public delegate void Tickeventhandler ();        public event Tickeventhandler TickEvent; <summary>///Create a Timer (constructor)///</summary>//<param name= "ThreadName" > Thread name < /param>//<param name= "TimeInterval" > Interval </param> public timerdemo (string threadname, int T imeinterval = Int. MaxValue) {this. ThreadName = ThreadName; This.            TimeInterval = TimeInterval; This.        Enabled = false; }///<summary>/////</summary> public void Run () {WH Ile (True) {//If the current timer is not enabled, it is detected at every interval if the IF (!this) is enabled.                    Enabled) {thread.sleep (100);                Continue                }//Trigger event TickEvent if (tickevent! = null) {tickevent ();                }//Hibernate for a certain time, waiting for the Next loop thread.sleep (timeinterval% 100);                    for (int temp = 0; temp < timeinterval/100; temp++) {thread.sleep (100); if (!this.                    Enabled) {break; }                }            }        }    }

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.