Home > C#.NET > Execute a method every time interval automatically c#

Execute a method every time interval automatically c#

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 🙂

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: