سؤال

ولقد وظيفة في Quartz.Net الذي يؤدي في كثير من الأحيان الى حد بعيد، وأحيانا تمتد لفترة طويلة، كيف يمكنني إلغاء الزناد إذا كان العمل قيد التشغيل بالفعل؟

هل كانت مفيدة؟

المحلول

والطريقة أكثر القياسية هو استخدام IInterruptableJob، انظر http://quartznet.sourceforge.net /faq.html#howtostopjob . بالطبع هذا هو مجرد طريقة أخرى لقول ما اذا (! jobRunning) ...

نصائح أخرى

هل يمكنك ليس فقط تعيين بعض نوع من متغير عمومي (jobRunning = صحيح) عندما يبدأ العمل والعودة إلى false بمجرد الانتهاء من ذلك؟

وبعد ذلك عندما الحرائق الزناد، فقط تشغيل التعليمات البرمجية الخاصة بك إذا (jobRunning == كاذبة)

وتطبيقك يمكن إزالة نفسه من قائمة الوظائف عند بدء التشغيل وإدراج نفسها على الاغلاق.

في الوقت الحاضر يمكن استخدام "WithMisfireHandlingInstructionIgnoreMisfires" في المشغل الخاص بك واستخدام [DisallowConcurrentExecution] سمة في عملك.

وكان هذا التطبيق الخاص بي (باستخدام الاقتراحات على الرابط الذي أعطى 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()));
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top