Timer in C#

The System.Timers.Timer class in C# generates events at specified intervals. It is useful for executing code periodically, such as updating displays, checking status, or performing background tasks.

The timer can be configured to fire once or repeatedly, and provides precise control over timing intervals and event handling.

Syntax

Following is the syntax for creating and configuring a timer −

Timer timer = new System.Timers.Timer(intervalInMilliseconds);
timer.Elapsed += EventHandlerMethod;
timer.Enabled = true;

Following is the syntax for the event handler method −

private static void EventHandlerMethod(Object source, ElapsedEventArgs e) {
    // Code to execute when timer elapses
}

Key Properties

Property Description
Interval Gets or sets the interval in milliseconds between timer events.
Enabled Gets or sets whether the timer should raise events.
AutoReset Gets or sets whether the timer should raise events repeatedly (true) or only once (false).

Using Timer for Periodic Events

The following example demonstrates a timer that fires every 2 seconds and displays the current time −

using System;
using System.Timers;

public class TimerExample {
    private static Timer timer;
    private static int counter = 0;

    public static void Main() {
        timer = new System.Timers.Timer(2000);
        timer.Elapsed += OnTimedEvent;
        timer.AutoReset = true;
        timer.Enabled = true;

        Console.WriteLine("Timer started. Will fire 5 times...");
        
        // Wait for 11 seconds to see the timer fire 5 times
        System.Threading.Thread.Sleep(11000);
        
        timer.Stop();
        Console.WriteLine("Timer stopped.");
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e) {
        counter++;
        Console.WriteLine("Timer fired #{0} at: {1}", counter, e.SignalTime.ToString("HH:mm:ss"));
        
        if (counter >= 5) {
            ((Timer)source).Stop();
        }
    }
}

The output of the above code is −

Timer started. Will fire 5 times...
Timer fired #1 at: 14:25:32
Timer fired #2 at: 14:25:34
Timer fired #3 at: 14:25:36
Timer fired #4 at: 14:25:38
Timer fired #5 at: 14:25:40
Timer stopped.

Using Timer with AutoReset False

When AutoReset is set to false, the timer fires only once −

using System;
using System.Timers;

public class OneTimeTimer {
    private static Timer timer;

    public static void Main() {
        timer = new System.Timers.Timer(3000);
        timer.Elapsed += OnTimedEvent;
        timer.AutoReset = false;
        timer.Enabled = true;

        Console.WriteLine("One-time timer started. Will fire once after 3 seconds...");
        
        System.Threading.Thread.Sleep(5000);
        Console.WriteLine("Program completed.");
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e) {
        Console.WriteLine("Timer fired once at: {0}", e.SignalTime.ToString("HH:mm:ss"));
    }
}

The output of the above code is −

One-time timer started. Will fire once after 3 seconds...
Timer fired once at: 14:25:43
Program completed.

Timer Control Methods

The timer provides several methods for controlling its execution −

using System;
using System.Timers;

public class TimerControl {
    private static Timer timer;

    public static void Main() {
        timer = new System.Timers.Timer(1000);
        timer.Elapsed += OnTimedEvent;
        timer.AutoReset = true;

        Console.WriteLine("Starting timer...");
        timer.Start();
        
        System.Threading.Thread.Sleep(3000);
        
        Console.WriteLine("Stopping timer...");
        timer.Stop();
        
        System.Threading.Thread.Sleep(2000);
        
        Console.WriteLine("Restarting timer...");
        timer.Start();
        
        System.Threading.Thread.Sleep(2000);
        timer.Close();
        Console.WriteLine("Timer closed.");
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e) {
        Console.WriteLine("Tick at: {0}", DateTime.Now.ToString("HH:mm:ss"));
    }
}

The output of the above code is −

Starting timer...
Tick at: 14:25:46
Tick at: 14:25:47
Tick at: 14:25:48
Stopping timer...
Restarting timer...
Tick at: 14:25:51
Tick at: 14:25:52
Timer closed.

Conclusion

The System.Timers.Timer class in C# provides a powerful way to execute code at regular intervals. It supports both one-time and recurring events, and offers precise control through properties like Interval, AutoReset, and methods like Start(), Stop(), and Close().

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements