Quartz.net Simple Example

。_饼干妹妹 提交于 2019-12-02 19:31:51

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!