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.
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.
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.
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.
It is an ASP.NET component that performs asynchronous or synchronous Web pages at a regular interval.
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.
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.

Join the conversation! Your thoughts help the community grow.