简体   繁体   中英

How to schedule tasks using Quartz.Net inside a Windows Service?

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.

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.

Has anyone done what I'm trying before? Any tips are appreciated.

PS. I don't want to use Quartz.Net's own Service.

One of the most common reasons a job doesn't execute, is because you need to call the Start() method on the scheduler instance.

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.

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

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:

    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

Cheers!

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.

Quartz.NET basic setup: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM