Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 235
Ketan Avatar asked Mar 23 '17 14:03

Ketan


1 Answers

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();
}
like image 114
Ofir Winegarten Avatar answered Sep 19 '22 08:09

Ofir Winegarten