using System; using System.Timers; class Program { static Timer timer = new Timer(5000); // 5 seconds static void Main(string[] args) { timer.Elapsed += OnTimedEvent; timer.Enabled = true; Console.WriteLine("Press any key to exit."); Console.ReadKey(); } static void OnTimedEvent(object sender, ElapsedEventArgs e) { Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); } }
using System; using System.Timers; class Program { static Timer timer = new Timer(); static void Main(string[] args) { timer.Elapsed += OnTimedEvent; timer.Interval = 1000; // 1 second timer.AutoReset = true; timer.Enabled = true; Console.WriteLine("Press any key to exit."); Console.ReadKey(); } static void OnTimedEvent(object sender, ElapsedEventArgs e) { Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); } }This example creates a Timer object with an interval of 1 second and sets the AutoReset property to true, which causes the timer to continue raising events every 1 second until it is stopped. When the event occurs, the OnTimedEvent method is called and a message is printed to the console. Package library: System.Timers.