Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz.net Simple Example

Tags:

c#

quartz.net

I'm trying to find a simple Quartz.Net example where when a button is clicked, it kicks off the Quartz.Net functionality.

I was able to take the Quartz.Net example (console application) and change some things to produce this (SimpleExample.cs):

    public virtual void Run()
    {
        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sched = sf.GetScheduler();

        DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTime.UtcNow);
        DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(null, 10);

        IJobDetail job = JobBuilder.Create<HelloJob>()
            .WithIdentity("job1", "group1")
            .Build();
        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .StartAt(runTime)
            .WithCronSchedule("5 0/1 * * * ?")
            .Build();

        sched.ScheduleJob(job, trigger);

        sched.Start();

    }

But I'm a little confused as to how one triggers this from a button click. I thought I could do something like this:

    private void button1_Click(object sender, EventArgs e)
    {
     code here....
    }

But that didn't work.

I reviewed the following websites, but not all were helpful in relation to starting this from a button click.

http://www.mkyong.com/java/quartz-scheduler-example/ - Java, so hard for me to understand the difference (I'm new to all this!).

http://www.hardcodet.net/2010/01/lightweight-task-slash-job-scheduling-with-silverlight-support - This was helpful, but it is unclear to me how Silverlight works with a regular .Net Form. Seems like an entirely different project.

/////

Additional changes: 10/14/2011

I reviewed the suggested code and found the following link with another (simple) example. http://simplequartzschedulerincsharp.blogspot.com/

I went ahead and built out a simple form with a few changes to the author's code as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quartz;
using Quartz.Impl;

//http://simplequartzschedulerincsharp.blogspot.com/

namespace QuartzExampleWF
{
    public partial class Form1 : Form
    {
        private static IScheduler _scheduler;

        public Form1()
        {
            InitializeComponent();

            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            _scheduler = schedulerFactory.GetScheduler();

            AddJob();
        }
        public static void AddJob()
        {
            IMyJob myJob = new MyJob();
            JobDetail jobDetail = new JobDetail("Job1", "Group1", myJob.GetType());
            CronTrigger trigger = new CronTrigger("Trigger1", "Group1", "5 0/1 * * * ?");
            _scheduler.ScheduleJob(jobDetail, trigger);
            DateTime? nextFireTime = trigger.GetNextFireTimeUtc();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            _scheduler.Start();

        }

        internal class MyJob : IMyJob
        {
            public void Execute(JobExecutionContext context)
            {
                DateTime now = DateTime.Now;

                DoMoreWork();
            }

            public void DoMoreWork()
            {
                //more code...
            }
        }
        internal interface IMyJob : IJob
        {
        }    
    }
 }

Ive never done a internal class before and ran into the issue of referencing a textbox within. For example, I am trying to do the following:

      public void Execute(JobExecutionContext context)
        {
            DateTime now = DateTime.Now;
            this.textbox1 = Now.value;
            DoMoreWork();
        }

But I cannot reference a textbox. I would have the same issue with a datagrid or a toolStripStatusLabel. What is the best way to access objects like a textbox or a toolStripStatusLabel under the above code?

like image 770
JAS Avatar asked Oct 11 '11 17:10

JAS


1 Answers

You could do something like this:

public partial class MainForm : Form
{
    IScheduler sched;
    IJobDetail job;

    public MainForm()
    {
        InitializeComponent();

        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sched = sf.GetScheduler();

        DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTime.UtcNow);
        DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(null, 10);

        job = JobBuilder.Create<HelloJob>()
            .WithIdentity("job1", "group1")
            .Build();
        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .StartAt(runTime)
            .WithCronSchedule("5 0/1 * * * ?")
            .Build();

        sched.ScheduleJob(job, trigger);
    }

    private void startScheduler_Click(object sender, EventArgs e)
    {
        sched.Start();
    }

    private void startJob_Click(object sender, EventArgs e)
    {
        sched.TriggerJob(job.Name, job.Group);
    }
}

It wasn't clear to me if you wanted the button to start the scheduler or start the job, so I added a button for both. The key is that you want to initialize the scheduler separately from starting it with the button click. The simplest place to initialize it would be in the constructor for the form.

like image 96
Jason Smale Avatar answered Oct 21 '22 08:10

Jason Smale