这段c#代码通过timer定时器每隔5秒钟调用一次OnTimerElapsed事件,在屏幕上输出信息,这是一个简单的timer定时器使用范例,可以大概了解一些C#中timer的用法
using System; using System.Timers; public class Program { private static System.Timers.Timer testTimer; public static void Main(string[] args) { testTimer = new System.Timers.Timer(5000); // 5 seconds testTimer.Elapsed += new ElapsedEventHandler(OnTimerElapsed); testTimer.Interval = 5000; testTimer.Enabled = true; Console.WriteLine("Press the enter key to stop the timer"); Console.ReadLine(); } private static void OnTimerElapsed(object source, ElapsedEventArgs e) { Console.WriteLine("Timer elapsed at {0}", e.SignalTime); } }