使用Unschedulejob修改触发器详细信息 - 我做错了什么? (Quartz.NET)

时间:2010-10-24 19:38:16

标签: c# asp.net quartz-scheduler quartz.net

我正在为我正在创建的系统使用Quartz.NET,其中作业通过Windows服务运行,并且可以在ASP.NET页面中添加和编辑(包含触发器详细信息)。

我遇到了一些问题 - 我需要能够修改触发器详细信息,例如simpletrigger或cronexpression的类型,并且我被告知我需要取消安排工作然后重新安排它。问题是,取消安排工作,然后重新安排完全取消了工作的触发器由于某种原因。以下是我在编辑页面上单击“提交”按钮时的代码:

 Trigger[] triggers = sched.GetTriggersOfJob(hdnID.Value, hdnGroupID.Value);

        if (ddlScheduleType.SelectedIndex == 1)
        {
            sched.UnscheduleJob(triggers[0].Name, triggers[0].Group);
            foreach (SimpleTrigger trigger in triggers.OfType<SimpleTrigger>())
            {
                if (ddlInterval.SelectedIndex == 0)
                {
                    trigger.RepeatInterval = TimeSpan.Parse("00:00:01");
                }
                else if (ddlInterval.SelectedIndex == 1)
                {
                    trigger.RepeatInterval = TimeSpan.Parse("00:01:00");
                }
                else if (ddlInterval.SelectedIndex == 2)
                {
                    trigger.RepeatInterval = TimeSpan.Parse("01:00:00");
                }


               sched.RescheduleJob(trigger.Name, trigger.Group, trigger);


            }
        }

        else
        {
            foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
            {
                trigger.CronExpressionString = txtCron.Text;
                sched.UnscheduleJob(triggers[0].Name, triggers[0].Group);
                sched.RescheduleJob(trigger.Name, trigger.Group, trigger);

            }


        }

        sched.AddJob(jobDetail, true);

为什么这不起作用的任何想法?感谢

NEW:

 if (ddlScheduleType.SelectedIndex == 1)

        {
            foreach (SimpleTrigger trigger in triggers.OfType<SimpleTrigger>())
            {
                Trigger newtrigger = new SimpleTrigger();


                if (ddlInterval.SelectedIndex == 0)
                {
                    newtrigger = TriggerUtils.MakeSecondlyTrigger();
                }
                else if (ddlInterval.SelectedIndex == 1)
                {
                    newtrigger = TriggerUtils.MakeMinutelyTrigger();
                }
                else if (ddlInterval.SelectedIndex == 2)
                {
                    newtrigger = TriggerUtils.MakeHourlyTrigger();
                }

                newtrigger.Name = triggers[0].Name;
                newtrigger.Group = triggers[0].Group;
                newtrigger.JobName = triggers[0].JobName;
                newtrigger.JobGroup = triggers[0].JobGroup;

                sched.RescheduleJob(trigger.Name, trigger.Group, newtrigger);


            }
        }

        else
        {
            foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
            {
                CronTrigger newtrigger2 = new CronTrigger();
                newtrigger2.Name = trigger.Name;
                newtrigger2.Group = trigger.Group;
                newtrigger2.JobName = trigger.JobName;
                newtrigger2.JobGroup = trigger.JobGroup;

                newtrigger2.CronExpressionString = txtCron.Text;
                sched.RescheduleJob(trigger.Name, trigger.Group, newtrigger2);

            }


        }

1 个答案:

答案 0 :(得分:2)

  • 我怀疑UnscheduleJob是必要的。重新安排应delete the old trigger,所以不用尝试
  • 您的else代码没有意义:

        foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
        {
            trigger.CronExpressionString = txtCron.Text;
            sched.UnscheduleJob(triggers[0].Name, triggers[0].Group);
            sched.RescheduleJob(trigger.Name, trigger.Group, trigger);
        }
    

    你的意思是

        foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
        {
            trigger.CronExpressionString = txtCron.Text;
            sched.UnscheduleJob(trigger.Name, trigger.Group); //I would remove this 
            sched.RescheduleJob(trigger.Name, trigger.Group, trigger);
        }
    
  • 最后,我会尝试使用 new 触发器重新安排,而不是使用旧的

  • 重新安排