Quartz.net简单示例

时间:2011-10-11 17:09:52

标签: c# quartz.net

我正在尝试找到一个简单的Quartz.Net示例,当单击一个按钮时,它会启动Quartz.Net功能。

我能够使用Quartz.Net示例(控制台应用程序)并更改一些内容来生成它(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();

    }

但是我对如何通过按钮点击触发这一点感到困惑。我以为我可以这样做:

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

但那没用。

我查看了以下网站,但并非所有网站都通过点击按钮启动此功能。

http://www.mkyong.com/java/quartz-scheduler-example/ - Java,我很难理解其中的差异(我对这一切都很陌生!)。

http://www.hardcodet.net/2010/01/lightweight-task-slash-job-scheduling-with-silverlight-support - 这很有帮助,但我不清楚Silverlight如何使用常规的.Net表单。看起来像一个完全不同的项目。

/////

其他更改:10/14/2011

我查看了建议的代码,并在另一个(简单)示例中找到了以下链接。 http://simplequartzschedulerincsharp.blogspot.com/

我继续构建了一个简单的表单,对作者的代码进行了一些更改,如下所示:

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
        {
        }    
    }
 }

我之前从未做过内部课程,并且遇到了引用文本框的问题。例如,我正在尝试执行以下操作:

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

但我无法引用文本框。我对datagrid或toolStripStatusLabel有同样的问题。在上述代码下访问文本框或toolStripStatusLabel等对象的最佳方法是什么?

1 个答案:

答案 0 :(得分:13)

你可以这样做:

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);
    }
}

我不清楚你是否想要按钮启动调度程序或启动作业,所以我为两者添加了一个按钮。关键是您要单独初始化调度程序,而不是单击按钮启动调度程序。初始化它的最简单的地方是在表单的构造函数中。