Skip to content

sgbj/crontimer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sgbj.Cron.CronTimer

Provides a cron timer similar to System.Threading.PeriodicTimer that enables waiting asynchronously for timer ticks.

Available on NuGet.

Usage

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));

Resources

About

Like PeriodicTimer but for cron jobs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages