如何检查是否某项工作在石英调度C#运行(How to check if a particular j

2019-10-30 04:06发布

我使用的石英调度安排写入根据有关触发器的基础用户输入一个文件的工作。 我要检查作业是否仍然在stop()方法运行。 如何检查作业是否仍在运行?

 public class JobScheduler
{
    static StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
    public IScheduler scheduler = schedulerFactory.GetScheduler().Result;
    public void start()
    {
        scheduler.Start();

        IJobDetail jobDetail = JobBuilder.Create<FileWritingJob>().WithIdentity("FileWriteJob","FileWriteGroup").Build();

        string date_time = System.Configuration.ConfigurationManager.AppSettings["datetime"];
        System.Diagnostics.Debug.WriteLine(date_time);
        DateTime dateTime = DateTime.Parse(date_time);
        System.Diagnostics.Debug.WriteLine("Parse datetime:"+dateTime);
        string frequency= System.Configuration.ConfigurationManager.AppSettings["frequency"];
        System.Diagnostics.Debug.WriteLine("Frequency:" + frequency);
        if (frequency.Equals("hourly", StringComparison.InvariantCultureIgnoreCase) && !frequency.Equals("default"))
        {
            ITrigger hourlyTrigger = TriggerBuilder.Create().WithIdentity("hourlyTrigger").WithCronSchedule("0 " + dateTime.Minute + " " + dateTime.Hour + "/1 1/1 * ? *").StartAt(DateBuilder.DateOf(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Day, dateTime.Month, dateTime.Year)).Build();
            scheduler.ScheduleJob(jobDetail, hourlyTrigger);
        }
        else if (frequency.Equals("daily", StringComparison.InvariantCultureIgnoreCase) && !frequency.Equals("default"))
        {
            ITrigger dailyTrigger = TriggerBuilder.Create().WithIdentity("dailyTrigger").WithCronSchedule("0 " + dateTime.Minute + " " + dateTime.Hour + " 1/1 * ? *").StartAt(DateBuilder.DateOf(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Day, dateTime.Month, dateTime.Year)).Build();
            scheduler.ScheduleJob(jobDetail, dailyTrigger);
        }
        else if (frequency.Equals("weekly", StringComparison.InvariantCultureIgnoreCase) && !frequency.Equals("default"))
        {
            ITrigger weeklyTrigger = TriggerBuilder.Create().WithIdentity("weeklyTrigger").WithCronSchedule("0 " + dateTime.Minute + " " + dateTime.Hour + " 1/7 * ? *").StartAt(DateBuilder.DateOf(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Day, dateTime.Month, dateTime.Year)).Build();
            scheduler.ScheduleJob(jobDetail, weeklyTrigger);
        }

    }
    public void stop()
    {
        scheduler.Shutdown().ConfigureAwait(false).GetAwaiter().GetResult();
    }
}

Answer 1:

在石英2.X你可以从调度得到所有执行作业,我没有3.x的测试,但我敢肯定它是相同的。

var executingJobs = scheduler.GetCurrentlyExecutingJobs();


文章来源: How to check if a particular job is running in quartz scheduler c#