下面的代码通过Timer定时器每隔1000毫秒(1秒)触发一次事件,
using System; using System.Timers; class TestTimer { public static void Main () { Timer timer = new Timer(); timer.Elapsed + = new ElapsedEventHandler(DisplayTimeEvent); timer.Interval = 1000; timer.Start(); timer.Enabled = true; while (Console.Read ()! = 'q') { //------------- } } public static void DisplayTimeEvent (object source, ElapsedEventArgs e) { Console.Write({0}, DateTime.Now); } }