Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 984
Roman Avatar asked Feb 10 '11 16:02

Roman


People also ask

How do you use a quartz scheduler?

These steps are as follows: In the first step, we have to initialize the scheduler instance from Quartz by using StdSchedulerFactory() method. After that, we start the scheduler instance with the Quartz API start() method. Start the scheduler instance with Quartz API start()

How do you schedule multiple jobs using Quartz?

If you want to schedule multiple jobs in your console application you can simply call Scheduler. ScheduleJob (IScheduler) passing the job and the trigger you've previously created: IJobDetail firstJob = JobBuilder. Create<FirstJob>() .


3 Answers

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.

like image 114
warriorpostman Avatar answered Sep 21 '22 22:09

warriorpostman


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!

like image 40
Marc D. Avatar answered Sep 22 '22 22:09

Marc D.


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

like image 27
Andrew Thompson Avatar answered Sep 24 '22 22:09

Andrew Thompson