How to use the FluentScheduler library for scheduling tasks in C #?

I am trying to get to know the C # FluentScheduler library through a console application (.NET Framework 4.5.2). Below is the code that wrote:

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 is executed without any errors, but I can not see anything on the console. Did I miss something?

+4
source share
2 answers

You are using the library incorrectly - you should not create a new one Schedule.
You must use the method that is inside 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 problem is that the console application will exit immediately after initialization, so add Console.ReadLine()

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

FluentScheduler - , ASP.Net, - , .

- Windows.

- , :

using System;
using FluentScheduler;

namespace SchedulerDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Start the scheduler
            JobManager.Initialize(new ScheduledJobRegistry());

            // Wait for something
            Console.WriteLine("Press enter to terminate...");
            Console.ReadLine();

            // Stop the scheduler
            JobManager.StopAndBlock();
        }
    }

    public class ScheduledJobRegistry : Registry
    {
        public ScheduledJobRegistry()
        {
            Schedule<MyJob>()
                    .NonReentrant() // Only one instance of the job can run at a time
                    .ToRunOnceAt(DateTime.Now.AddSeconds(3))    // Delay startup for a while
                    .AndEvery(2).Seconds();     // Interval

            // TODO... Add more schedules here
        }
    }

    public class MyJob : IJob
    {
        public void Execute()
        {
            // Execute your scheduled task here
            Console.WriteLine("The time is {0:HH:mm:ss}", DateTime.Now);
        }
    }
}
+1

Source: https://habr.com/ru/post/1673013/


All Articles