介绍
这篇文章给大家分享的是有关c#中定时器定时器的使用示例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
static void Main (string [], args) ,,,,,,,{ ,,,,,,,,,,,# region ,定时器 ,,,,,,,,,,,TimerDemo td =, new TimerDemo (“TimerDemo",, 1000); ,,,,,,,,,,,td.Enabled =,真的; ,,,,,,,,,,,td.TickEvent +=, TestHandler; ,,,,,,,,,,,Thread timer =, new 线程(td.Run); ,,,,,,,,,,,timer.Start (); ,,,,,,,,,,,# endregion ,,,,,,,,,,,Console.ReadLine (); ,,,,,,,} ,,,,,,,///,& lt; summary> ,,,,,,,///,测试用事件 ,,,,,,,///,& lt;/summary> ,,,,,,,static void TestHandler () ,,,,,,,{ ,,,,,,,,,,,Console.WriteLine (DateTime.Now.ToLongTimeString ()); ,,,,,,,}
public class TimerDemo ,,,, ,,,{ ,,,,,,,//线程名 ,,,,,,,string _ThreadName; ,,,,,,,public string ThreadName ,,,,,,,{ ,,,,,,,,,,,get {_ThreadName;, return } ,,,,,,,,,,,private set {=, _ThreadName 价值;,} ,,,,,,,} ,,,,,,,//时间间隔 ,,,,,,,int _TimeInterval; ,,,,,,,public int TimeInterval ,,,,,,,{ ,,,,,,,,,,,get {_TimeInterval;, return } ,,,,,,,,,,,set {=, _TimeInterval 价值;,} ,,,,,,,} ,,,,,,,//当前计时器是否启用,真的:启用,错误的:不启用 ,,,,,,,bool _Enabled; ,,,,,,,public bool 启用 ,,,,,,,{ ,,,,,,,,,,,get {_Enabled;, return } ,,,,,,,,,,,set {=, _Enabled 价值;,} ,,,,,,,} ,,,,,,,//每隔一段时间需要运行的事件 ,,,,,,,public delegate void TickEventHandler (); ,,,,,,,public event TickEventHandler TickEvent; ,,,,,,,///,& lt; summary> ,,,,,,,///,建立一个计时器(构造函数) ,,,,,,,///,& lt;/summary> ,,,,,,,///,& lt; param name=癟hreadName"祝辞线程名& lt;/param> ,,,,,,,///,& lt; param name=癟imeInterval"祝辞时间间隔& lt;/param> ,,,,,,,public TimerDemo (string ThreadName, int TimeInterval =, int.MaxValue) ,,,,,,,{ ,,,,,,,,,,,this.ThreadName =, ThreadName; ,,,,,,,,,,,this.TimeInterval =, TimeInterval; ,,,,,,,,,,,this.Enabled =,假; ,,,,,,,} ,,,,,,,///,& lt; summary> ,,,,,,,///,定期执行事件 ,,,,,,,///,& lt;/summary> null null null null null null null null null null null null null null null null null null null null null null null null null null null nullc#中定时器定时器的使用示例