Load Data Interval Basis Using Timer(Thread) In C#

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.

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.

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.

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.

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.
  1. private Timer timerObj = null; 
  2. private DateTime lastUpdateTS = DateTime.MinValue;  
  3. public int refreshInterval { getset; }  
  5. public void RefreshData()  
  6. {  
  7.     try  
  8.     {  
  9.         this.timerObj = new Timer(LoadData, null, Timeout.Infinite, Timeout.Infinite);  
  10.         this.timerObj.Change(0, Timeout.Infinite);  
  11.     }  
  12.     catch (Exception ex)  
  13.     {  
  14.         //Log error     
  15.     }  

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.
  1. private void LoadData(object state)  
  2. {  
  3.     try  
  4.     {  
  5.         TimeSpan elapsed = DateTime.Now - this.lastUpdateTS;  
  6.         if (elapsed.TotalMinutes >= this.refreshInterval)  
  7.         {  
  8.             // Load value from database     
  9.             ProcessRecord();  
  11.             this.lastUpdateTS = DateTime.Now;  
  12.         }  
  14.         // 6 seconds interval to call the method again..  
  15.         this.timerObj.Change(6000, Timeout.Infinite);  
  16.     }  
  17.     catch (Exception ex)  
  18.     {  
  19.         // log exception    
  20.     }  

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.
  1. private void ProcessRecord()    
  2. {    
  3.     Console.WriteLine(DateTime.Now.ToString());
  4. }  
Figure 1: Output of the current time through Timer
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.