Это была моя реализация (с использованием предложений по ссылке, которую 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()));
}
}