正确的结束Quartz调度程序

时间:2018-07-04 05:32:57

标签: c# multithreading quartz-scheduler

我有Windows窗体应用程序,单击该按钮后,它会启动一些预定的作业并执行某些操作。结束调度程序的正确方法是什么,以便每个线程都能正确终止?

为简单起见,假设以下代码(基本上是Quartz文档中的教程,仅作了少量更改):

   public partial class Form1 : Form
    {
        IScheduler scheduler;

        public Form1()
        {
            InitializeComponent();
        }

        private void start_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {

                scheduler = StdSchedulerFactory.GetDefaultScheduler();
                scheduler.Start();

                IJobDetail job = JobBuilder.Create<HelloJob>()
                    .StoreDurably()
                    .WithIdentity("job1", "group1")
                    .Build();

                scheduler.AddJob(job, true);

                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(3)
                        .RepeatForever())
                    .ForJob(job)
                    .Build();

                ITrigger trigger2 = TriggerBuilder.Create()
                    .WithIdentity("trigger2", "group2")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(4)
                        .RepeatForever())
                    .ForJob(job)
                    .Build();

                scheduler.ScheduleJob(trigger);
                scheduler.ScheduleJob(trigger2);

            }
            catch (SchedulerException e)
            {
                Console.WriteLine(e);
            }
        }


        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (scheduler != null)
                scheduler.Shutdown();
        }
    }

    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Greetings from HelloJob!");
        }
    }

}

一切似乎都很好,单击按钮后创建并启动了调度程序,并在关闭表单后将其关闭。但是,我不确定关闭调度程序是否会终止并清除调度程序创建的所有内容。

谢谢!

0 个答案:

没有答案