How To Run A Timer On Specific Time Interval

Introduction

This blog demonstrates, how to run a timer on the specific time interval, using System.Threading.Timer class. This will help the developer to execute a task on the certain time interval. This blog starts with the simple declaration of Timer and will execute a method with a console message printing.

Step 1: Create a new console Application with the name “Threading_Timer” and declare the ‘ _timer’ of type System.Threading.Timer and a local variable ‘count’ of type int globally.

Declaration of Timer and a local variable of type int

  1. //Declare a _timer of type System.Threading  
  2. private static Timer _timer;  
  3.   
  4. //Local Variable of type int  
  5. private static int count = 1;  
Step 2: Initialize the ‘_timer’ in Main method and call a callback method ‘callTimerMethode()’ to print some message at the timer execution interval, inside this.
  1. //Initialization of _timer   
  2. _timer = new Timer(x => { callTimerMethode(); }, null, Timeout.Infinite, Timeout.Infinite);   
Step 3: Define callTimerMethode() as following:
  1. /// <summary>  
  2. /// This method will print timer executed time and increase the count with 1.  
  3. /// </summary>  
  4. private static void callTimerMethode()   
  5. {  
  6.     Console.WriteLine(string.Format("Timer Executed {0} times.", count));  
  7.     count = count + 1;  
  8. }  
Step 4: Define a method called ‘Setup_Timer()’ to set the timer execution time and change the execution time of the timer on this time interval and call ‘Setup_Timer()’ method in Main method.
  1. /// <summary>  
  2. /// This method will set the timer execution time and will change the tick  
  3. time of timer.  
  4.     /// </summary>  
  5. private static void Setup_Timer()  
  6. {  
  7.     DateTime currentTime = DateTime.Now;  
  8.     DateTime timerRunningTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 2, 0, 0);  
  9.     timerRunningTime = timerRunningTime.AddDays(15);  
  10.     double tickTime = (double)(timerRunningTime - DateTime.Now).TotalSeconds;  
  11.     _timer.Change(TimeSpan.FromSeconds(tickTime), TimeSpan.FromSeconds(tickTime));  
  12. }  
The method, shown above, will set the timer running time at the interval of every 15 days at 2 AM.

If you want to run the timer after every 2 minutes, just comment these line of code,
  1. //DateTime currentTime = DateTime.Now;  
  2. //DateTime timerRunningTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 2, 0, 0);  
  3. //timerRunningTime = timerRunningTime.AddDays(15);  
Add the line of code, given below:
  1. DateTime timerRunningTime = DateTime.Now.AddMinutes(2);  
Complete Program line of Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading;  
  6.   
  7. namespace Threading_Timer  
  8. {  
  9.     public class Program  
  10.     {  
  11.         //Declare a _timer of type System.Threading  
  12.         private static Timer _timer;  
  13.   
  14.         //Local Variable of type int  
  15.         private static int count = 1;  
  16.   
  17.         static void Main(string[] args)  
  18.         {  
  19.             //Initialization of _timer   
  20.             _timer = new Timer(x => { callTimerMethode(); }, null, Timeout.Infinite, Timeout.Infinite);  
  21.             Setup_Timer();  
  22.             Console.ReadKey();  
  23.         }  
  24.   
  25.         /// <summary>  
  26.         /// This method will print timer executed time and increase the count  
  27.             with 1.   
  28.         /// </summary>  
  29.         private static void callTimerMethode()  
  30.         {  
  31.             Console.WriteLine(string.Format("Timer Executed {0}   
  32.             times.",count));  
  33.             count=count+1;  
  34.         }  
  35.   
  36.         /// <summary>  
  37.         /// This method will set the timer execution time and will change the   
  38.             tick time of timer.  
  39.         /// </summary>  
  40.         private static void Setup_Timer()  
  41.         {  
  42.             DateTime currentTime = DateTime.Now;  
  43.             DateTime timerRunningTime = new DateTime(currentTime.Year,  
  44.             currentTime.Month, currentTime.Day, 2, 0, 0);  
  45.             timerRunningTime = timerRunningTime.AddDays(15);  
  46.             //DateTime timerRunningTime = DateTime.Now.AddMinutes(2);  
  47.   
  48.             double tickTime = (double)(timerRunningTime –   
  49.             DateTime.Now).TotalSeconds;  
  50.   
  51.             _timer.Change(TimeSpan.FromSeconds(tickTime),  
  52.  TimeSpan.FromSeconds(tickTime));  
  53.         }  
  54.     }  
  55. }  
The output looks, as shown in Figure 1:

output

Summary

In this blog, I discussed, how we can set the execution time of a method, using “System.Threading.Timer” class. I think this will help a developer to execute a specific task at a specified time interval.