我正在尝试通过一个控制台应用程序( C# FluentScheduler 4.5.2)来熟悉.Net框架库。下面是已经写好的代码:
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();
}
}这段代码执行时没有任何错误,但我在控制台上看不到任何内容。我是不是漏掉了什么?
发布于 2017-03-24 00:39:00
您正在以错误的方式使用库-您不应该创建新的Schedule。
您应该使用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();
}
}第二个问题是控制台应用程序将在初始化后立即退出,因此添加一个Console.ReadLine()
static void Main(string[] args)
{
JobManager.Initialize(new MyRegistry());
Console.ReadLine();
}发布于 2017-03-24 00:38:27
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);
}
}
}https://stackoverflow.com/questions/42978573
复制相似问题