Archive

Archive for November, 2013

Execute a method every time interval automatically c#

November 8, 2013 Leave a comment

To call a method automatically for every time interval, we can use timers in C#

See the code below.

class Program
{
public static System.Timers.Timer aTimer;
static void Main(string[] args)
{
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(RunThis);
aTimer.AutoReset = true;
aTimer.Enabled = true;
Console.ReadLine();
}
private static void RunThis(object source, ElapsedEventArgs e)
{
Console.WriteLine("Print this in every 10 seconds");
}
}

Happy coding 🙂