如何在 Windows 服务中使用 Quartz.Net 安排任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4959581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to schedule tasks using Quartz.Net inside a Windows Service?
提问by Roman
I have created a windows service project in VS and in it I configure Quartz.Net to run a task immediately. The code that registers the task runs with no exception, but the task is never executed as far as my debugging can tell.
我在 VS 中创建了一个 Windows 服务项目,并在其中配置 Quartz.Net 以立即运行任务。注册任务的代码无一例外地运行,但就我的调试而言,该任务从未执行过。
I can't be sure because debugging a Windows Service is very different. The way I do it is to programatically launching the debugger from my code. Quartz.Net runs jobs on a separate threads, but I'm not sure if VS2010 can see other running threads when debugging a Windows Service.
我不能确定,因为调试 Windows 服务非常不同。我这样做的方法是从我的代码中以编程方式启动调试器。Quartz.Net 在单独的线程上运行作业,但我不确定 VS2010 在调试 Windows 服务时是否可以看到其他正在运行的线程。
Has anyone done what I'm trying before? Any tips are appreciated.
有没有人做过我以前尝试过的事情?任何提示表示赞赏。
PS. I don't want to use Quartz.Net's own Service.
附注。我不想使用 Quartz.Net 自己的服务。
回答by warriorpostman
One of the most common reasons a job doesn't execute, is because you need to call the Start() method on the scheduler instance.
作业不执行的最常见原因之一是因为您需要在调度程序实例上调用 Start() 方法。
http://quartznet.sourceforge.net/faq.html#whytriggerisntfiring
http://quartznet.sourceforge.net/faq.html#whytriggerisntfiring
But it's hard to say what the problem is if we don't have some sort of snippet of the code that does the scheduler creation and job registration.
但是,如果我们没有执行调度程序创建和作业注册的某种代码片段,则很难说问题是什么。
回答by Marc D.
I see that this is a bit dated, but it came up many times in various searches!
我看到这有点过时了,但它在各种搜索中出现了很多次!
Definitely check out this article, which uses an XML config when the scheduler is instantiated. http://miscellaneousrecipesfordotnet.blogspot.com/2012/09/quick-sample-to-schedule-tasks-using.html
一定要看看这篇文章,它在调度程序被实例化时使用了一个 XML 配置。 http://miscellaneousrecipesfordotnet.blogspot.com/2012/09/quick-sample-to-schedule-tasks-using.html
In case you would rather not use XML (dynamically created tasks and such), replace the "Run" procedure from the article above with something like this:
如果您不想使用 XML(动态创建的任务等),请将上面文章中的“运行”过程替换为以下内容:
public void Run()
{
// construct a scheduler factory
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
_scheduler = schedulerFactory.GetScheduler();
IJobDetail job = JobBuilder.Create<TaskOne>()
.WithIdentity("TaskOne", "TaskOneGroup")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("TaskOne", "TaskOneGroup")
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(20).RepeatForever())
.Build();
_scheduler.ScheduleJob(job, trigger);
_scheduler.TriggerJob(job.Key);
_scheduler.Start();
}
Note- Using Quartz .NET 2.1.2, .NET 4
注- 使用 Quartz .NET 2.1.2、.NET 4
Cheers!
干杯!
回答by Andrew Thompson
I have successfully used Quart.NET before in a Windows service. When the service starts-up I create the Scheduler Factory and then get the Scheduler. I then start the scheduler which implicitly reads in the configuration XML I have specified in the App.config of the service.
我之前在 Windows 服务中成功使用过 Quart.NET。当服务启动时,我创建调度程序工厂,然后获取调度程序。然后我启动调度程序,它隐式读取我在服务的 App.config 中指定的配置 XML。
Quartz.NET basic setup: http://quartznet.sourceforge.net/tutorial/lesson_1.html
Quartz.NET 基本设置:http://quartznet.sourceforge.net/tutorial/lesson_1.html
App.config Setup Question: http://groups.google.com/group/quartznet/browse_thread/thread/abbfbc1b65e20d63/b1c55cf5dabd3acd?lnk=gst&q=%3Cquartz%3E#b1c55cf5dabd3acd
App.config 设置问题:http://groups.google.com/group/quartznet/browse_thread/thread/abbfbc1b65e20d63/b1c55cf5dabd3acd?lnk=gst&q=%3Cquartz%3E# b1c55cf5dabd3acd