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. //Local Variable of type int
  4. 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. namespace Threading_Timer
  7. {
  8. public class Program
  9. {
  10. //Declare a _timer of type System.Threading
  11. private static Timer _timer;
  12. //Local Variable of type int
  13. private static int count = 1;
  14. static void Main(string[] args)
  15. {
  16. //Initialization of _timer
  17. _timer = new Timer(x => { callTimerMethode(); }, null, Timeout.Infinite, Timeout.Infinite);
  18. Setup_Timer();
  19. Console.ReadKey();
  20. }
  21. /// <summary>
  22. /// This method will print timer executed time and increase the count
  23. with 1.
  24. /// </summary>
  25. private static void callTimerMethode()
  26. {
  27. Console.WriteLine(string.Format("Timer Executed {0}
  28. times.",count));
  29. count=count+1;
  30. }
  31. /// <summary>
  32. /// This method will set the timer execution time and will change the
  33. tick time of timer.
  34. /// </summary>
  35. private static void Setup_Timer()
  36. {
  37. DateTime currentTime = DateTime.Now;
  38. DateTime timerRunningTime = new DateTime(currentTime.Year,
  39. currentTime.Month, currentTime.Day, 2, 0, 0);
  40. timerRunningTime = timerRunningTime.AddDays(15);
  41. //DateTime timerRunningTime = DateTime.Now.AddMinutes(2);
  42. double tickTime = (double)(timerRunningTime –
  43. DateTime.Now).TotalSeconds;
  44. _timer.Change(TimeSpan.FromSeconds(tickTime),
  45. TimeSpan.FromSeconds(tickTime));
  46. }
  47. }
  48. }
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.