Quartz.net 工作似乎在 Windows 服务结束后仍然存在
本文关键字:结束 存在 服务 Windows 工作 net Quartz | 更新日期: 2023-09-27 18:37:14
我是 Quartz.net 新手,目前只是对它有所了解。我正在设置一个 Quartz.net 作业以从我的 Windows 服务运行。
我的 Windows 服务有两种启动模式 a) 如果项目作为 Windows 服务运行,它将作为普通服务运行。b) 如果项目在 Visual Studio 中以调试模式运行,它将以交互模式运行(这样我就可以根据上下文将调试信息输出到控制台和记录器)。它在 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 以调试模式运行项目时,当我停止服务时,控制台显示"服务已停止",但 syncJob Quartz.net 似乎继续运行(控制台输出继续)为什么会这样?
您需要
更新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服务的拆解和设置,而无需您在问题开始时显示的所有样板位。