Skip to content

MichalMoudry/fs-cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FsCron

A .NET library for periodically running tasks. This library was built on top of Cronos.

Basic usage

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

Async usage

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.

Types of jobs

  • 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.

Job cancellation

Scheduler class realizes IDisposable (and IAsyncDisposable) interface. Also, the class internally stores a CancellationTokenSource which is cancelled and disposed along side the scheduler itself.

AOT compatibility

About

A .NET library for periodically running tasks

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published