Introduction
Sometimes, we get a requirement to perform an operation on an interval basis. For instance, there is a requirement to refresh a dataset in 15 minute intervals. Here, Timer class will help to perform any operation on an interval basis.
.NET Framework provides 4 types of Timers, each has its different functionality.
System.Threading.Timer
It executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. This class is for use as a Server-based or Service component in a multithreaded environment. It is present in System.Threading namespace of assembly mscorlib.dll.
System.Timers.Timer
It fires an event and executes the code in one or more event sinks at the regular intervals. The class is for use as a Server-based or Service component in a multithreaded environment. The differences between thread timer and timers timer are given below.
- Timers.Timer is thread-safe but Threading timer is not thread safe.
- Initial timer event can be scheduled in a Threading timer but not in Timers.Timer.
- Timers.Timer supports inheritance but Threading.Timers doesn't support it.
System.Windows.Forms.Timer
It is a Windows Forms component that fires an event and executes the code in one or more event sinks at regular intervals. It is for use in a single-threaded environment. It executes on the UI thread.
System.Web.UI.Timer
It is an ASP.NET component that performs asynchronous or synchronous Web pages at a regular interval.
For more on timer, please visit here. Here, we will discuss about Threading.Timer class.
Using Code
Step 1
Declare variables for Timer, current datetime and timer interval. Subsequently, it initializes the timer object with passing the required parameters like callback method (LoadData), object state as null, due time, and period as Timeout.Infinite.
- private Timer timerObj = null;
- private DateTime lastUpdateTS = DateTime.MinValue;
- public int refreshInterval { get; set; }
-
- public void RefreshData()
- {
- try
- {
- this.timerObj = new Timer(LoadData, null, Timeout.Infinite, Timeout.Infinite);
- this.timerObj.Change(0, Timeout.Infinite);
- }
- catch (Exception ex)
- {
-
- }
- }
Step 2 It is time to define callback function (LoadData()), which receives an object type as a parameter. Now, it calls another method to display the current time. Here, I am calling another function and you can also write logic here instead of method call. Subsequently, it sets the timer object to 6 second intervals, which means it calls the LoadData() in a 6 second interval basis.
- private void LoadData(object state)
- {
- try
- {
- TimeSpan elapsed = DateTime.Now - this.lastUpdateTS;
- if (elapsed.TotalMinutes >= this.refreshInterval)
- {
-
- ProcessRecord();
-
- this.lastUpdateTS = DateTime.Now;
- }
-
-
- this.timerObj.Change(6000, Timeout.Infinite);
- }
- catch (Exception ex)
- {
-
- }
- }
Step 3 In Step 2, it calls ProcessRecord() method. Here, it displays only the current time stamp. You need to write your own logic as per your requirement. The Output is shown in Figure 1.
- private void ProcessRecord()
- {
- Console.WriteLine(DateTime.Now.ToString());
- }
Figure 1: Output of the current time through Timer
Conclusion
In .NET Framework, there are four types of Timers which you can use as per the requirement. Here, we discussed about Thread Timer. It is useful when you want to perform any background operation on an interval basis.