What’s the most reliable way to implement a timer in C# for precise interval execution?

I’m looking to implement a timer in C# that can consistently execute a method at a fixed interval—specifically every 15 seconds, with minimal drift or misfires. I don’t need ultra-high precision (nanoseconds aren’t necessary), but I’d like the callback to run as close to the target interval as possible (e.g., within 14.51 to 15.49 seconds).

I’ve seen multiple timer options in C#, like System.Timers.Timer, System.Threading.Timer, and Task.Delay in async loops, but I’m not sure which one is best suited for accuracy and reliability over long periods.

A code sample would be helpful, along with any best practices to avoid drift or delay buildup. What’s the most stable approach for implementing a C# timer in this case

In my experience working with production services, I’ve found that the C# timer using System.Threading.Timer is often the most reliable and lightweight choice for fixed-interval execution. It’s especially effective when you’re working outside of UI threads, where you don’t have to worry about the overhead of UI updates.

Here’s a simple example:

var timer = new System.Threading.Timer(state =>
{
    Console.WriteLine($"Tick at {DateTime.Now}");
}, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));

This setup ensures minimal CPU usage and delivers consistent results over long periods. However, it’s important to keep an eye on the work inside the callback. If your method takes longer than 15 seconds to execute, the callback will overlap, which can cause drift. But for simple cases like yours, it’s solid and dependable.

That’s a great point, @macy-davis ! If you’re looking for even more control, especially when you need async logic, cancellation tokens, or drift correction, I usually prefer an async loop. This approach is modern and keeps things efficient without blocking threads, which can be crucial in worker-based apps or services that need to run continuously without interruptions.

Here’s a pattern I use for a C# timer with async support:

public async Task StartPreciseTimer(CancellationToken token)
{
    var interval = TimeSpan.FromSeconds(15);
    
    while (!token.IsCancellationRequested)
    {
        var sw = Stopwatch.StartNew();

        // Your timed logic
        Console.WriteLine($"Tick at {DateTime.Now}");

        sw.Stop();
        var delay = interval - sw.Elapsed;
        if (delay > TimeSpan.Zero)
            await Task.Delay(delay, token);
    }
}

By using Stopwatch, we measure the time taken by the logic and adjust the delay accordingly to maintain consistency. This helps keep the execution as close to the target interval as possible, adapting to the variability of the callback execution time.

@joe-elmoufak , I completely agree with the async approach, but for simpler scenarios or apps where you want something clean and event-driven, a C# timer using System.Timers.Timer might still be your best bet. It’s especially useful in Windows Forms or service apps that rely on event-based triggers. Plus, it’s super reliable in most environments and doesn’t require async overhead.

Here’s how you can use it:

var timer = new System.Timers.Timer(15000); // milliseconds
timer.Elapsed += (s, e) =>
{
    Console.WriteLine($"Timer ticked at {e.SignalTime}");
};
timer.AutoReset = true;
timer.Start();

The System.Timers.Timer runs on a thread pool, which allows it to fire at precise intervals unless the system is under heavy load. It’s generally accurate for most use cases, so for basic tasks, it’s a perfect solution. If you’re just handling simple events and don’t need fine-tuned control over execution time, this method works great.