【问题标题】:Configuring Quartz.Net to stop a job from executing, if it is taking longer than specified time span配置 Quartz.Net 以停止作业的执行,如果它花费的时间超过指定的时间跨度
【发布时间】:2014-06-27 07:24:05
【问题描述】:

我正在制作一个调度器,就像使用 Quartz.Net 的 Windows 调度器一样。

在 Windows 计划程序中,有一个选项可以在任务花费超过指定时间时停止运行。我必须在我的调度程序中实现相同的功能。

但我找不到任何扩展方法/设置来相应地配置触发器或作业。

我请求一些意见或建议。

【问题讨论】:

    标签: asp.net triggers scheduler jobs quartz.net


    【解决方案1】:

    您可以编写小代码来设置在另一个线程上运行的自定义超时。实现 IInterruptableJob 接口并在作业应该被中断时从该线程调用其 Interrupt() 方法。您可以根据需要修改以下示例代码。请在需要时进行必要的检查/配置输入。

    public class MyCustomJob : IInterruptableJob
        {
            private Thread runner;
            public void Execute(IJobExecutionContext context)
            {
                int timeOutInMinutes = 20; //Read this from some config or db.
                TimeSpan timeout = TimeSpan.FromMinutes(timeOutInMinutes);
                //Run your job here.
                //As your job needs to be interrupted, let us create a new task for that.
                var task = new Task(() =>
                    {
                        Thread.Sleep(timeout);
                        Interrupt();
                    });
    
                task.Start();
                runner = new Thread(PerformScheduledWork);
                runner.Start();
            }
    
            private void PerformScheduledWork()
            {
                //Do what you wish to do in the schedled task.
    
            }
    
            public void Interrupt()
            {
                try
                {
                    runner.Abort();
                }
                catch (Exception)
                {
                   //log it! 
    
                }
                finally
                {
                    //do what you wish to do as a clean up task.
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2023-02-07
      • 2023-01-30
      • 1970-01-01
      • 2014-07-29
      • 2013-03-11
      相关资源
      最近更新 更多