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
-
- private static Timer _timer;
-
-
- 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.
-
- _timer = new Timer(x => { callTimerMethode(); }, null, Timeout.Infinite, Timeout.Infinite);
Step 3: Define callTimerMethode() as following:
-
-
-
- private static void callTimerMethode()
- {
- Console.WriteLine(string.Format("Timer Executed {0} times.", count));
- count = count + 1;
- }
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.
-
-
- time of timer.
-
- private static void Setup_Timer()
- {
- DateTime currentTime = DateTime.Now;
- DateTime timerRunningTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 2, 0, 0);
- timerRunningTime = timerRunningTime.AddDays(15);
- double tickTime = (double)(timerRunningTime - DateTime.Now).TotalSeconds;
- _timer.Change(TimeSpan.FromSeconds(tickTime), TimeSpan.FromSeconds(tickTime));
- }
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,
Add the line of code, given below:
- DateTime timerRunningTime = DateTime.Now.AddMinutes(2);
Complete Program line of Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
-
- namespace Threading_Timer
- {
- public class Program
- {
-
- private static Timer _timer;
-
-
- private static int count = 1;
-
- static void Main(string[] args)
- {
-
- _timer = new Timer(x => { callTimerMethode(); }, null, Timeout.Infinite, Timeout.Infinite);
- Setup_Timer();
- Console.ReadKey();
- }
-
-
-
- with 1.
-
- private static void callTimerMethode()
- {
- Console.WriteLine(string.Format("Timer Executed {0}
- times.",count));
- count=count+1;
- }
-
-
-
- tick time of timer.
-
- private static void Setup_Timer()
- {
- DateTime currentTime = DateTime.Now;
- DateTime timerRunningTime = new DateTime(currentTime.Year,
- currentTime.Month, currentTime.Day, 2, 0, 0);
- timerRunningTime = timerRunningTime.AddDays(15);
-
-
- double tickTime = (double)(timerRunningTime –
- DateTime.Now).TotalSeconds;
-
- _timer.Change(TimeSpan.FromSeconds(tickTime),
- TimeSpan.FromSeconds(tickTime));
- }
- }
- }
The output looks, as shown in Figure 1:
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.