Task Scheduler in C# (.NET): Quartz.NET, Hangfire, Coravel

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Task Scheduler in C# (.NET): Overview and Examples



In modern C# (.NET) development, there is often a need to execute code on a schedule: send email newsletters, update cache, archive logs, or run ETL processes. Specialized libraries — Job Schedulers — exist for this purpose. In this article, we will review three popular solutions: Quartz.NET, Hangfire, and Coravel, and provide examples of their usage.



1. Quartz.NET — Powerful and Flexible Scheduler



Quartz.NET is a port of the popular Java framework Quartz. It supports complex schedules using cron expressions, job persistence in a database, clustering, and triggers.



Installation

dotnet add package Quartz


Example: Simple Job with a Cron Schedule

using Quartz;using Quartz.Impl;

public class HelloJob : IJob{ public async Task Execute(IJobExecutionContext context) { Console.WriteLine($"Hello! Time: {DateTime.Now}"); }}

// Scheduler setupISchedulerFactory schedulerFactory = new StdSchedulerFactory();IScheduler scheduler = await schedulerFactory.GetScheduler();

// Define the jobIJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("helloJob", "group1") .Build();

// Trigger: every 10 secondsITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .WithCronSchedule("0/10 * * * * ?") // every 10 seconds .ForJob(job) .Build();

await scheduler.ScheduleJob(job, trigger);await scheduler.Start();


Features:

  • Support for cron expressions (e.g., 0 0 12 * * ? — every day at noon).
  • Job storage in ADO.NET (SQL Server, PostgreSQL, MySQL).
  • Ability to dynamically create/delete jobs.
  • Clustering for fault tolerance.


2. Hangfire — Simplicity and Built-in Dashboard



Hangfire is a library for executing background tasks with minimal configuration. It provides a built-in web dashboard for monitoring, automatic retries on errors, and integration with ASP.NET Core.



Installation

dotnet add package Hangfiredotnet add package Hangfire.SqlServer # for SQL Server storage


Example: Delayed and Recurring Task

using Hangfire;

// Setup in Startup.cspublic void ConfigureServices(IServiceCollection services){ services.AddHangfire(config => config.UseSqlServerStorage("connection_string")); services.AddHangfireServer();}

public void Configure(IApplicationBuilder app){ app.UseHangfireDashboard(); // /hangfire // Recurring job: every 5 minutes RecurringJob.AddOrUpdate( "cleanup-cache", () => Console.WriteLine("Cleaning cache..."), "*/5 * * * *"); // Delayed job (after 1 hour) BackgroundJob.Schedule( () => SendEmail("user@example.com"), TimeSpan.FromHours(1));}

public void SendEmail(string email){ Console.WriteLine($"Sending email to {email}");}


Advantages:

  • Built-in dashboard (/hangfire) for viewing job status.
  • Automatic retries on failures.
  • Support for queues (default: Redis or SQL Server).
  • Simple integration with ASP.NET Core via DI.


3. Coravel — Lightweight Scheduler for .NET Core



Coravel is a minimalistic library that requires no external dependencies (other than .NET itself). It is ideal for small projects where persistence is not needed.



Installation

dotnet add                    

Recommendations