停止Quartz的异步作业

问题描述

我刚发现Quartz,目前正在尝试在我的项目中实现它。我很难找到任何有效的教程或异步作业的实现示例。我已使其正常运行/正在运行,但现在我希望能够停止该过程。

我已经找到此页面https://www.blexin.com/en-US/Article/Blog/Stopping-asynchronous-Jobs-of-Quartz-3-61

但是提供的示例对我而言并不十分清楚。有人可以提供一些停止进程的示例吗?我在WPF中有两个按钮StartStop。我想将Stop流程包装到一个方法中,以便可以通过单击按钮来触发它。

这是我当前的代码

using Quartz;
using Quartz.Impl;
using System;
using System.Threading.Tasks;
using System.Windows;

namespace Importer_WPF
{
    public class JobScheduler
    {
        private static ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

        public async void Start()
        {
            IScheduler scheduler = await schedulerFactory.GetScheduler();
            await scheduler.Start();

            IJobDetail job = JobBuilder.Create<HelloJob>().Build();

            // Trigger the job to run Now,and then repeat every 10 seconds
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("HelloJob","GreetingGroup")
                .WithDailyTimeIntervalSchedule(s =>
                             s.WithIntervalInMinutes(1)
                              .OnMondayThroughFriday()
                              .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(9,0))
                              .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(17,0)))
               .Build();

            await scheduler.ScheduleJob(job,trigger);

            // Configure the cancellation of the schedule job with jobkey
            await Task.Delay(TimeSpan.FromSeconds(1));
        }

        public void Stop()
        {
            IScheduler scheduler = (IScheduler)schedulerFactory.GetScheduler().GetAwaiter().GetResult();
            _ = scheduler.PauseJob(new JobKey("HelloJob","GreetingGroup"));
        }

    }

    public class HelloJob : IJob
    {
        Task IJob.Execute(IJobExecutionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            Task taskA = new Task(() => MessageBox.Show("Hello from task at {0}",DateTime.Now.ToString()));
            taskA.Start();
            return taskA;
        }
    }
}

我当前的代码运行没有错误,但是我的任务并未在Stop方法触发时停止。它继续运行。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...