A .NET library for periodically running tasks. This library was built on top of Cronos.
await using var scheduler = new Scheduler(TimeZoneInfo.Local);
scheduler.NewAsyncJob(
"* * * * *",
async token =>
{
Console.WriteLine($"[{DateTimeOffset.Now}] Test print");
await Task.Delay(1000, token);
}
);
scheduler.Start();Asynchronous usage is about running the scheduler in a separate thread.
await using var scheduler = new Scheduler(TimeZoneInfo.Local);
scheduler.NewAsyncJob(
"* * * * *",
async token =>
{
Console.WriteLine($"[{DateTimeOffset.Now}] Test print");
await Task.Delay(1000, token);
}
);
scheduler.StartAsync();Internally, Scheduler class has two methods Start() and StartAsync(). Start() executes startInternal() method directly and StartAsync() treats startInternal() as a ThreadStart delegate for a background Thread.
- Async job - A Func delegate that has one parameter (cancellation token) and returns a Task. The returned task is not awaited, but rather started on a thread pool.
- Sync job: An Action delegate that is periodically queued on the thread pool, so that it is not blocking scheduler's own execution.
Scheduler class realizes IDisposable (and IAsyncDisposable) interface. Also, the class internally stores a CancellationTokenSource which is cancelled and disposed along side the scheduler itself.