0Pricing
C# Academy · Lesson

Periodic Tasks & Timers

Schedule recurring work with PeriodicTimer, System.Threading.Timer, and manage cancellation gracefully.

Scheduling Recurring Work

Many background services need to run on a fixed schedule — every 5 minutes, every hour, or daily. .NET provides several mechanisms: PeriodicTimer (modern), System.Threading.Timer (classic), and Quartz.NET (cron-based).

PeriodicTimer: The Modern Choice

PeriodicTimer (introduced in .NET 6) is designed for use in async methods. It fires exactly on schedule without callbacks and integrates cleanly with cancellation.

public class MetricsCollectorService : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken ct)
    {
        using var timer = new PeriodicTimer(TimeSpan.FromMinutes(1));

        while (await timer.WaitForNextTickAsync(ct))
        {
            // Runs every 60 seconds
            await CollectMetricsAsync(ct);
        }
        // Loop exits cleanly when ct is cancelled
    }
}

All lessons in this course

  1. IHostedService & BackgroundService
  2. Worker Service Projects
  3. Periodic Tasks & Timers
  4. Quartz.NET Scheduled Jobs
← Back to C# Academy