2009-09-10 92 views

回答

2

更標準的方法是使用IInterruptableJob,請參閱http://quartznet.sourceforge.net/faq.html#howtostopjob。當然,這只是另一種說法,如果(!jobRunning)...

+0

只是一次實現IInterruptableJob的類的一個實例,或者是使用該類的Job? – 2009-09-15 08:49:36

+2

如果您只想一次只允許一個實例,那麼IStatefulJob就是您的炸彈,http://quartznet.sourceforge.net/faq.html#howtopreventconcurrentfire。 IInterruptableJob只是給你一個標準的信號中斷方法,你需要在工作方面做重大的事情(檢查中斷標誌是否已經產生)。 – 2009-09-15 13:27:23

1

當作業開始時你能不能只是設置某種全局變量(jobRunning = true),並且一旦完成就將其恢復爲假?

然後當觸發器觸發,只要運行你的代碼,如果(jobRunning ==假)

+0

是的,通常最簡單的解決方案是最好的。我已經實現了這一點,我雖然有一個實施的解決方案,這暫停了工作,直到第一次完成,但無論如何,這工作得很好! – 2009-09-10 14:38:45

0

您的應用可以從啓動時的工作列表中刪除本身並插入本身關機。

0

如今,您可以在觸發器中使用「WithMisfireHandlingInstructionIgnoreMisfires」,並在作業中使用[DisallowConcurrentExecution]屬性。

0

這是我的實現(使用MarkoL先前給出的鏈接上的建議)。

我只是想保存一些打字。

我是Quartz.NET的新手,所以拿下一串鹽。

public class AnInterruptableJob : IJob, IInterruptableJob 
{ 

    private bool _isInterrupted = false; 

    private int MAXIMUM_JOB_RUN_SECONDS = 10; 

    /// <summary> 
    /// Called by the <see cref="IScheduler" /> when a 
    /// <see cref="ITrigger" /> fires that is associated with 
    /// the <see cref="IJob" />. 
    /// </summary> 
    public virtual void Execute(IJobExecutionContext context) 
    { 


     /* See http://aziegler71.wordpress.com/2012/04/25/quartz-net-example/ */ 

     JobKey key = context.JobDetail.Key; 

     JobDataMap dataMap = context.JobDetail.JobDataMap; 

     int timeOutSeconds = dataMap.GetInt("TimeOutSeconds"); 
     if (timeOutSeconds <= 0) 
     { 
      timeOutSeconds = MAXIMUM_JOB_RUN_SECONDS; 
     } 

     Timer t = new Timer(TimerCallback, context, timeOutSeconds * 1000, 0); 


     Console.WriteLine(string.Format("AnInterruptableJob Start : JobKey='{0}', timeOutSeconds='{1}' at '{2}'", key, timeOutSeconds, DateTime.Now.ToLongTimeString())); 


     try 
     { 
      Thread.Sleep(TimeSpan.FromSeconds(7)); 
     } 
     catch (ThreadInterruptedException) 
     { 
     } 


     if (_isInterrupted) 
     { 
      Console.WriteLine("Interrupted. Leaving Excecute Method."); 
      return; 
     } 

     Console.WriteLine(string.Format("End AnInterruptableJob (should not see this) : JobKey='{0}', timeOutSeconds='{1}' at '{2}'", key, timeOutSeconds, DateTime.Now.ToLongTimeString())); 

    } 


    private void TimerCallback(Object o) 
    { 
     IJobExecutionContext context = o as IJobExecutionContext; 

     if (null != context) 
     { 
      context.Scheduler.Interrupt(context.FireInstanceId); 
     } 
    } 

    public void Interrupt() 
    { 
     _isInterrupted = true; 
     Console.WriteLine(string.Format("AnInterruptableJob.Interrupt called at '{0}'", DateTime.Now.ToLongTimeString())); 
    } 
} 
相關問題