Language: EN


Create periodic tasks in C# with FluentScheduler

FluentScheduler is a .NET library that allows the creation of tasks at a specified frequency using a Fluent syntax.

Sometimes we have to make certain tasks run at certain times in some programs. For example, reloading data every 15 minutes, running a task at 8:00 am, or performing a specific action on Saturdays.

In general, it is quite boring code to write, and prone to failure. This is where FluentScheduler comes in, allowing us to define this type of tasks, easily.

The truth is that the syntax is truly wonderful. It has a lot of extension methods to customize our needs, using a “Fluent” syntax.

It is a very useful library for asynchronous task automation in .NET applications.

How to use FluentScheduler

We can easily add the library to a .NET project, through the corresponding Nuget package.

Install-Package FluentScheduler

Now, its simplest use would be the following,

using FluentScheduler;


    () => Console.WriteLine("5 minutes just passed."),
    s => s.ToRunEvery(5).Minutes()

 JobManager.JobEnd += (info) => Logger.Information(
	info.Duration > TimeSpan.FromSeconds(1) ?
	$"{info.Name}: ended ({info.Duration})" :
	$"{info.Name}: ended"

In more complex cases, we can define a class with the configuration we want,

using FluentScheduler;

public class MyRegistry : Registry
    public MyRegistry()
            Schedule(() => Console.WriteLine("Every 2 seconds ")).ToRunNow().AndEvery(2).Seconds();

        Schedule<MyJob>(() => Console.WriteLine("5 seconds have passed")).ToRunOnceIn(5).Seconds();

        Schedule(() => Console.WriteLine("It's 9:15 PM")).ToRunEvery(1).Days().At(21, 15);
        Schedule(() => Console.WriteLine("It's 3:00 am on the first Monday of the month")).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

And execute it in the JobManager

JobManager.Initialize(new MyRegistry());

Additionally, we can execute actions when a Job starts or finishes, or if an exception occurs.

JobManager.JobException += info => Logger.Error("An error just happened with a scheduled job: " + info.Exception);
You can also listen for jobs start and end:

JobManager.JobStart += info => Logger.Information($"{info.Name}: started");
JobManager.JobEnd += info => Logger.Information($"{info.Name}: ended ({info.Duration})");

FluentScheduler is Open Source, and all the code and documentation is available on the project page and in its repository