当石英抛物线超出范围时会发生什么?

时间:2017-08-22 23:28:44

标签: c# quartz.net

当石英抛物线超出范围时会发生什么?垃圾收集器会杀死它还是会继续运行? Samle代码:

namespace Quartz_test
{
    public class SimpleJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("hello there");
        }
    }

    class Program
    {
        static void ScheduleTask()
        {
            // construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            // get a scheduler
            IScheduler scheduler = schedFact.GetScheduler();
            scheduler.Start();
            IJobDetail job = JobBuilder.Create<SimpleJob>()
                .WithIdentity("myJob", "group1")
                .Build();
            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(3))
                .Build();
            scheduler.ScheduleJob(job, trigger);
        }

        static void Main(string[] args)
        {
            ScheduleTask();
            //link to scheduler is lost here
            //how to stop SimpleJob execution?
            //Console.ReadLine();
        }
    }

此代码输出:

hello there
hello there
hello there

一切似乎都好,但即使我最后评论Console.ReadLine();,为什么程序也永远不会完成?

最后一个问题:如果我丢失了所有指向我的追随者的链接,如何阻止IJob执行

1 个答案:

答案 0 :(得分:1)

库中有一个关机方法。

syntax

void Shutdown( bool waitForJobsToComplete);

你可以使用它。

IScheduler scheduler = schedFact.GetDefaultScheduler();
scheduler.Shutdown(false);

您还可以使用UnscheduleJobDeleteJob方法。

scheduler.unscheduleJob(new TriggerKey("myJob", "group1"));

scheduler.deleteJob(new JobKey("myJob", "group1"));

这是包含调试信息的程序。我借用了here

的代码
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Quartz_test
{
    public class SimpleJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("hello there");
        }
    }

    class Program
    {

        private static void GetAllJobs(IScheduler scheduler)
        {
            IList<string> jobGroups = scheduler.GetJobGroupNames();
            // IList<string> triggerGroups = scheduler.GetTriggerGroupNames();
            Console.Write("START ===== \n is start? " + scheduler.IsStarted);
            Console.WriteLine("is stop? " + scheduler.IsShutdown);
            foreach (string group in jobGroups)
            {
                var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
                var jobKeys = scheduler.GetJobKeys(groupMatcher);
                Console.WriteLine();
                foreach (var jobKey in jobKeys)
                {
                    var detail = scheduler.GetJobDetail(jobKey);
                    var triggers = scheduler.GetTriggersOfJob(jobKey);
                    foreach (ITrigger trigger in triggers)
                    {
                        Console.WriteLine("group : " + group);
                        Console.WriteLine("name : " + jobKey.Name);
                        Console.WriteLine("desc : " + detail.Description);
                        Console.WriteLine("trg name : " + trigger.Key.Name);
                        Console.WriteLine("trg group : " + trigger.Key.Group);
                        Console.WriteLine("trg type : " + trigger.GetType().Name);
                        Console.WriteLine("trg state : " + scheduler.GetTriggerState(trigger.Key));
                        DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc();
                        if (nextFireTime.HasValue)
                        {
                            Console.WriteLine("next trigger? : " + nextFireTime.Value.LocalDateTime.ToString());
                        }

                        DateTimeOffset? previousFireTime = trigger.GetPreviousFireTimeUtc();
                        if (previousFireTime.HasValue)
                        {
                            Console.WriteLine("previous trigger? : " + previousFireTime.Value.LocalDateTime.ToString());
                        }
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine("===== END \n");
        }

        static void ScheduleTask() 
        {
            IScheduler scheduler = null;
            IJobDetail job = null;
            ITrigger trigger = null;

            try
            {
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();
                // get a scheduler
                scheduler = schedFact.GetScheduler();
                scheduler.Start();

                job = JobBuilder.Create<SimpleJob>()
                    .WithIdentity("myJob", "group1")
                    .Build();
                trigger = TriggerBuilder.Create()
                    .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(3))
                    .Build();

                scheduler.ScheduleJob(job, trigger);

                GetAllJobs(scheduler);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException.ToString());
            }
            finally
            {
                GetAllJobs(scheduler);

                TimeSpan spn = TimeSpan.FromSeconds(100L);
                Thread.Sleep(spn);


                scheduler.Shutdown(true);

            }

        }

        static void Main(string[] args)
        {
            ScheduleTask();
            //link to scheduler is lost here
            //how to stop SimpleJob execution?
            //Console.ReadLine();
        }
    }
}

祝你好运, Yaros