Quartz.net 简单示例 [英] Quartz.net Simple Example

查看:125
本文介绍了Quartz.net 简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个简单的 Quartz.Net 示例,当单击按钮时,它会启动 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.

我能够采用 Quartz.Net 示例(控制台应用程序)并更改一些内容以生成此示例 (SimpleExample.cs):

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

但这没有用.

我查看了以下网站,但并非所有网站都对通过单击按钮启动此网站有所帮助.

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,我很难理解其中的区别(我对这一切都很陌生!).

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- 这很有帮助,但我不清楚 Silverlight 如何与常规 .Net 表单一起工作.似乎是一个完全不同的项目.

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.

/////

其他更改:2011 年 10 月 14 日

Additional changes: 10/14/2011

我查看了建议的代码,发现以下链接与另一个(简单)示例.http://simplequartzschedulerincsharp.blogspot.com/

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

但我无法引用文本框.对于数据网格或 toolStripStatusLabel,我也会遇到同样的问题.在上述代码下访问文本框或 toolStripStatusLabel 等对象的最佳方式是什么?

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?

推荐答案

你可以这样做:

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.

这篇关于Quartz.net 简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆