问题描述
我的工作做调度,就像Windows调度使用Quartz.Net。
I am working on making a scheduler, just like Windows Scheduler using Quartz.Net.
在Windows计划中,有一个选项,以停止运行一个任务,如果时间超过规定时间以上。我要实现同样的在我的调度。
In Windows Scheduler, there is an option to stop a task from running if it takes more than the specified time. I have to implement the same in my scheduler.
但我无法找到任何扩展方法/设置相应地配置触发或作业。
But I am not able to find any extension method/setting to configure Trigger or Job accordingly.
我要求一些投入或建议吧。
I request some inputs or suggestions for it.
推荐答案
您可以写小code设置自定义timout另一个线程上运行。实现IInterruptableJob接口,并从该线程作出其中断()方法调用时,工作应中断。您可以修改下面的示例code,按您的需要。需要的地方请必要的检查/配置输入。
You can write small code to set a custom timout running on another thread. Implement IInterruptableJob interface and make a call to its Interrupt() method from that thread when the job should be interrupted. You can modify the following sample code as per your need. Please make necessary checks/config inputs wherever required.
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.
}
}
}
这篇关于配置Quartz.Net从执行停止作业,如果超过规定的时间跨度要长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!