1.C# Timer用法及實例詳解
http://developer.51cto.com/art/200909/149829.htm
http://www.cnblogs.com/OpenCoder/archive/2010/02/23/1672043.html
關於C# Timer類 在C#里關於定時器類就有3個
C# Timer使用的方法1.定義在System.Windows.Forms里
C# Timer使用的方法2.定義在System.Threading.Timer類里 "
C# Timer使用的方法3.定義在System.Timers.Timer類里
◆System.Windows.Forms.Timer
應用於WinForm中的,它是通過Windows消息機制實現的,類似於VB或Delphi中的Timer控件,內部使用API SetTimer實現的。它的主要缺點是計時不精確,而且必須有消息循環,Console Application(控制台應用程序)無法使用。
◆System.Timers.Timer
和System.Threading.Timer非常類似,它們是通過.NET Thread Pool實現的,輕量,計時精確,對應用程序、消息沒有特別的要求。
◆System.Timers.Timer還可以應用於WinForm,完全取代上面的Timer控件。它們的缺點是不支持直接的拖放,需要手工編碼。
C# Timer用法實例
使用System.Timers.Timer類
System.Timers.Timer t = new System.Timers.Timer(10000); //實例化Timer類,設置間隔時間為10000毫秒; t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到達時間的時候執行事件; t.AutoReset = true; //設置是執行一次(false)還是一直執行(true); t.Enabled = true; //是否執行System.Timers.Timer.Elapsed事件; public void theout(object source, System.Timers.ElapsedEventArgs e) { MessageBox.Show("OK!"); }
以上用法類似於winform,如下
1.將timer拖到界面上,設定屬性值,時間間隔等;
2.在事件中雙擊自動生成如theout的方法;
3. 調用方法,在要調用的按鈕事件等內,timer.start(),timer.stop() 這樣就行了。