Timer Class in C# example
In the following program, a thread with a timer is created. The Start and Stop methods of the timer are used to start and stop the timer and the thread.
Example:
using System; using System.Collections.Generic; using System.Text; using System.Timers; namespace ThreadTimers { class Test { static void Main() { Timer t1=new Timer(); t1.Interval=1000; t1.Elapsed+=t1_Elapsed; t1.Start(); Console.ReadLine(); t1.Stop(); Console.WriteLine("Timer Thread Stopped !"); Console.ReadLine(); t1.Start(); Console.ReadLine(); Console.WriteLine("Thread Destroyed !"); t1.Dispose(); } static void Display(object objTime) { Console.WriteLine(ObjTime); } static void t1_Elapsed(object sender, EventArgs e) { Console.Write("The Time is passing by: "); Console.WriteLine(DateTime.Now); } } }