Search code examples
c#fluentscheduler

How to use FluentScheduler library to schedule tasks in C#?


I am trying to get familiar with C# FluentScheduler library through a console application (.Net Framework 4.5.2). Below is the code that have written:

class Program
{
    static void Main(string[] args)
    {
        JobManager.Initialize(new MyRegistry());
    }
}


public class MyRegistry : Registry
{
    public MyRegistry()
    {
        Action someMethod = new Action(() =>
        {
            Console.WriteLine("Timed Task - Will run now");
        });

        Schedule schedule = new Schedule(someMethod);

        schedule.ToRunNow();


    }
}

This code executes without any errors but I don't see anything written on Console. Am I missing something here?


Solution

  • You are using the library the wrong way - you should not create a new Schedule.
    You should use the Method that is within the Registry.

    public class MyRegistry : Registry
    {
        public MyRegistry()
        {
            Action someMethod = new Action(() =>
            {
                Console.WriteLine("Timed Task - Will run now");
            });
    
            // Schedule schedule = new Schedule(someMethod);
            // schedule.ToRunNow();
    
            this.Schedule(someMethod).ToRunNow();
        }
    }
    

    The second issue is that the console application will immediately exit after the initialization, so add a Console.ReadLine()

    static void Main(string[] args)
    {
        JobManager.Initialize(new MyRegistry());
        Console.ReadLine();
    }