Provides a cron timer similar to System.Threading.PeriodicTimer
that enables waiting asynchronously for timer ticks.
Available on NuGet.
Normal usage:
// Every minute
using var timer = new CronTimer("* * * * *");
while (await timer.WaitForNextTickAsync())
{
// Do work
}
Example hosted service:
public class CronJob : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Every day at 8am local time
using var timer = new CronTimer("0 8 * * *", TimeZoneInfo.Local);
while (await timer.WaitForNextTickAsync(stoppingToken))
{
// Do work
}
}
}
Non-standard cron expression:
// Every 30 seconds
using var timer = new CronTimer(CronExpression.Parse("*/30 * * * * *", CronFormat.IncludeSeconds));
- Hanfire/Cronos - Library for working with cron expressions.
- Crontab.guru - The cron schedule expression editor.