Quartz.net 简单示例

发布于 2024-12-09 01:39:18 字数 3595 浏览 0 评论 0原文

我试图找到一个简单的 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 表单配合使用。看起来像是一个完全不同的项目。

/////

其他更改:2011 年 10 月 14 日

我查看了建议的代码,并找到了以下链接和另一个(简单)示例。 表单

我继续使用一些内容构建了一个简单的 对作者代码的更改如下:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2024-12-16 01:39:18

你可以这样做:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文