如何在 Quartz.Net 中使 Job 引发 EventHandler? [英] How to make a Job raise an EventHandler in Quartz.Net?
问题描述
我有一个控制台应用程序可以实例化 Quartz 调度程序.
我想要一个 Job 来引发一个事件处理程序,以便Mother App"调用特定的方法.
问题是工作类似乎与外部类明显隔离.
我确信有一个很好的方法可以做到这一点,但我还没有偶然发现.
I have a Console Application that instantiates a Quartz Scheduler.
I would like a Job to raise an event Handler so that the "Mother App" calls a particular Method.
The problem is that the Job Class seems to be pretty isolated to the external classes apparently.
I am sure there is a good way to do this but I did not stumble upon it yet.
public class RestartJob : IJob
{
public RestartJob()
{
}
public virtual void Execute(IJobExecutionContext context)
{
//Send Restart EventHandler Subscription to Console.
}
}
推荐答案
我通过在包含调度程序逻辑的类上实现单例解决了我的问题.很有魅力.希望这可以帮助其他人解决在我看来是必备"功能的问题.
I solved my problem by implementing a singleton on the class containing the Scheduler Logic. Works a charm. Hope this could help other to solve what seems to me as a "Must-Have" feature.
public class Skeduler
{
private static Skeduler instance;
public static Skeduler Instance
{
get
{
if (instance == null)
{
instance = new Skeduler();
}
return instance;
}
}
public delegate void SendRestartX();
public event SendRestartX SendRestart;
public void doSendRestart()
{
if (SendRestart!=null)
SendRestart();
}
//(Job Methods & Logics Goes Here)
}
public class RestartJob : IJob
{
//Required
public RestartJob()
{
}
public virtual void Execute(IJobExecutionContext context)
{
Skeduler.Instance.doSendRestart();
}
}
用法:
public MainClass
{
public void Run()
{
skeduler = Skeduler.Instance;
skeduler.SendRestart += new Skeduler.SendRestartX(MethodToCall);
}
}
这篇关于如何在 Quartz.Net 中使 Job 引发 EventHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!