我在 Quartz.Net 中有一份工作经常触发,有时会运行很长时间,如果工作已经在运行,我该如何取消触发器?
问问题
4121 次
5 回答
2
更标准的方法是使用 IInterruptableJob,参见http://quartznet.sourceforge.net/faq.html#howtostopjob。当然,这只是另一种说法 if (!jobRunning)...
于 2009-09-12T13:03:53.247 回答
1
您能否在作业开始时设置某种全局变量 (jobRunning=true) 并在作业完成后将其恢复为 false?
然后当触发器触发时,只需运行您的代码 if(jobRunning==false)
于 2009-09-10T13:40:40.713 回答
0
您的应用程序可以在启动时将自己从作业列表中删除,并在关机时插入自己。
于 2009-09-10T14:34:46.980 回答
0
现在,您可以在触发器中使用“WithMisfireHandlingInstructionIgnoreMisfires”,并在工作中使用 [DisallowConcurrentExecution] 属性。
于 2013-11-26T14:36:48.530 回答
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()));
}
}
于 2014-01-30T14:54:40.940 回答