2010-10-24 53 views
3

我正在使用Quartz.NET作爲我創建的系統,其中作業通過Windows服務運行,並且可以在ASP.NET頁面中添加和編輯(包含觸發器詳細信息)。使用Unschedulejob修改觸發器的詳細信息 - 我做錯了什麼? (Quartz.NET)

雖然我有一些問題 - 我需要能夠修改觸發器的詳細信息,例如簡單觸發器或cronexpression的類型,而且我被告知需要暫時不調度工作,然後重新計劃它。問題是,沒有計劃工作,然後重新安排由於某種原因完全從工作中刪除觸發器。這裏是我在編輯頁面點擊Submit按鈕時的代碼:

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

任何想法爲什麼這不起作用?由於

新:

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

      } 


     } 

回答

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); 
        } 
    
  • 最後,我想嘗試用新補賽觸發器而不是與舊的修改器

+0

乾杯好友,這很好 - 但如果我想改變一個SimpleTrigger到一個CronTrigger它似乎不工作,它只是保留舊的 – Chris 2010-10-25 13:14:59

+0

你可以發佈你用來代替SimpleTrigger的代碼CronTrigger? – 2010-10-25 13:16:38

+0

剛發佈新代碼:) – Chris 2010-10-25 13:38:14