我是 Quartz.net 的新手,目前只是对它有所了解。我正在设置一个 Quartz.net 作业以从我的 Windows 服务运行。
我的 Windows 服务有两种启动模式 a) 如果项目作为 Windows 服务运行,它将作为普通服务运行。 b) 如果项目在 Visual Studio 中以 Debug模式运行,它将以交互模式运行(这样我就可以根据上下文将调试信息输出到控制台而不是记录器)。它在 Main() 中按如下方式执行此操作(只是摘录):
if (Environment.UserInteractive && System.Diagnostics.Debugger.IsAttached) {
System.Reflection.MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
onStartMethod.Invoke(myService, new object[] { new string[] { } });
Console.WriteLine("Service started.");
Console.WriteLine("Press a key to stop service and finish process...");
Console.ReadKey();
System.Reflection.MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
onStopMethod.Invoke(myService, null);
Console.WriteLine("Service stopped.");
} else {
ServiceBase.Run(myService);
}
现在在 MyService 中我有以下工作,而且这个工作似乎按预期正确运行:
protected override void OnStart(string[] args)
{
_schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = _schedulerFactory.GetScheduler();
scheduler.Start();
IJobDetail syncJob = JobBuilder.Create<MySyncJob>()
.WithIdentity("syncJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(5)))
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
scheduler.ScheduleJob(syncJob, trigger);
}
然而,当我从我的 Visual Studio 以 Debug模式运行我的项目时,当我停止服务时,控制台显示“服务已停止”,但 Quartz.net syncJob 似乎继续运行(控制台输出继续)为什么会这样是吗?
最佳答案
您需要更新 OnStop
方法以在服务停止时关闭调度程序。
private ISchedulerFactory _schedulerFactory;
private IScheduler _scheduler;
protected override void OnStart(string[] args)
{
_schedulerFactory = new StdSchedulerFactory();
_scheduler = _schedulerFactory.GetScheduler();
_scheduler.Start();
IJobDetail syncJob = JobBuilder.Create<MySyncJob>()
.WithIdentity("syncJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(5)))
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
scheduler.ScheduleJob(syncJob, trigger);
}
protected override void OnStop()
{
// true parameter indicates whether to wait for running jobs
// to complete before completely tearing down the scheduler
// change to false to force running jobs to abort.
_scheduler.Shutdown(true);
}
作为旁注,您可能需要考虑使用 Topshelf这有助于处理 Windows 服务的拆卸和设置,而无需您在问题开始时显示的所有样板位。
关于c# - Windows 服务结束后,Quartz.net 作业似乎仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787163/