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

一个人想着一个人 提交于 2019-12-06 01:28:36

问题


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?


回答1:


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();
}



回答2:


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);
        }
    }
}


来源:https://stackoverflow.com/questions/42978573/how-to-use-fluentscheduler-library-to-schedule-tasks-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!