In c#, timer component is useful to raise an event repeatedly in our application based on the specified interval of time.
The timer component is available with System.Timers
namespace. So, to work with the timer component in our application to raise an event after a set of interval we need to import the System.Timers namespace.
Following is the example of defining the timer object that raises an elapsed event after a set of intervals in c#.
// create a timer
Timer timer = new Timer();
timer.Interval = 2000;
timer.Elapsed += TimerEvent;
timer.AutoReset = true;
timer.Enabled = true;
If you observe the above code, we created a timer object by using the Timer
class. Here, the Interval
property is used to specify the time interval to raise an Elapsed event (TimerEvent) and the Enabled
property is used to specify whether the timer object should raise an Elapsed event or not.
The AutoReset
property is used to configure the timer object to raise an elapsed event repeatedly at the specified interval of time defined by the Interval
property.
In case, if we want to raise the Elapsed event only once after the specified interval has elapsed, then we need to set the AutoReset
property to false
.
Following table lists some of the most commonly used properties of Timer class in c#.
Property | Description |
---|---|
AutoReset | It is useful to get or set whether the Timer should raise the Elapsed event only once (false) or repeatedly (true). |
CanRaiseEvents | It will return a value that indicates whether the component can raise an event or not. |
DesignMode | It will return a value that indicates whether the component is in design mode or not. |
Enabled | It is useful to get or set a value to indicate whether the Timer should raise the Elapsed event or not. |
Interval | It is useful to get or set the interval in milliseconds at which to raise the Elapsed event. |
Following table lists some of the most commonly used methods of Timer class in c#.
Method | Description |
---|---|
Start() | It is useful to start raise the Elapsed event by setting Enabled to true. |
Stop() | It will stop raising the Elapsed event by setting Enabled to false. |
Close() | It is useful to release the resources that are used by the Timer. |
Dispose() | It is useful to release all the resources used by the Component. |
MemberwiseClone() | It is useful to create a shallow copy of the current object. |
GetType() | It is useful to get the type of current instance. |
Following is the example of using Timer
class in c# to execute the Elapsed events repeatedly at specified interval of time.
using System;
using System.Timers;
namespace TutlaneExamples
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Present Enter Key to Exit the Application");
// create a timer
Timer timer = new Timer();
timer.Interval = 2000;
timer.Elapsed += TimerEvent;
timer.AutoReset = true;
timer.Enabled = true;
Console.ReadLine();
}
// Elapsed Event
static void TimerEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Event Raised at {0:HH:mm:ss.fff}", e.SignalTime);
}
}
}
If you observe the above example, we imported System.Timers
namespace to create a timer object by using Timer
class. Here, we used Interval
property to specify the time interval to raise an Elapsed event (TimerEvent) and the Enabled
property to specify whether the timer object should raise an Elapsed event or not.
When you execute the above program, you will get the result like as shown below.
Present Enter Key to Exit the Application
Event Raised at 07:48:49.274
Event Raised at 07:48:51.275
Event Raised at 07:48:53.275
Event Raised at 07:48:55.275
If you observe the result, for every 2 seconds the Elapsed event (TimerEvent) is raising and executing the elapsed event code.
This is how we can use the timer object to raise an event repeatedly at the specified interval of time-based on our requirements.