7

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?

Ketan
  • 373
  • 1
  • 6
  • 18
  • you should have running asp .net service to schedule jobs – Maksim Simkin Mar 23 '17 at 14:15
  • @MaksimSimkin Thanks for the reply. Do you mean I need to create a Windows service? I was under the impression that with Fluent Scheduler we don't need a Windows service. – Ketan Mar 23 '17 at 15:10
  • you should create an asp.net project and host it for example in your iis, look here: https://github.com/fluentscheduler/FluentScheduler#using-it-with-aspnet – Maksim Simkin Mar 23 '17 at 15:12
  • @MaksimSimkin you can use Fluent Scheduler with a console application. It not bound in any way to asp.net – Ofir Winegarten Mar 23 '17 at 16:41

2 Answers2

11

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();
}
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
10

FluentScheduler is a great package, but I'd avoid trying to use it in an ASP.Net app as suggested in the comments - when your app unloads after a period of inactivity your scheduler effectively stops.

A much better idea is to host it in a dedicated windows service.

That aside - you've asked for a Console App implementation, so give this a try:

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);
        }
    }
}
Pete
  • 1,807
  • 1
  • 16
  • 32