开发者

Using Unschedulejob to modify a triggers details - what am I doing wrong? (Quartz.NET)

I'm using Quartz.NET for a system I am creating, where jobs are run via a Windows Service and can be added and editted (inlcuding trigger details) in an ASP.NET page.

I'm having some issues though - I need to be able to modify a triggers details, such as the type of simpletrigger or the cronexpression, and I've been told that I need to unschedule the job then reschedule it. Problem is, unscheduling the job then rescheduling is totally removing the trigger from the job for some reason. Here is my code for when the Submit button is clicked on the edit page:

 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);

Any ideas why this isn't working? Thanks

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);

            }


        }


  • I doubt UnscheduleJob is necessary. Reschedule should delete the old trigger, so try without it
  • Your else code doesn't make sense:

        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);
        }
    

    Do you mean perhaps

        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);
        }
    
  • Lastly, I would try rescheduling with a new trigger rather than with the old one modified

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜