Quartz.Net触发事件

编程入门 行业动态 更新时间:2024-10-28 18:22:16
本文介绍了Quartz.Net触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有自己的 ITrigger .基本上,如下所示:

I have my own ITrigger. Basically, it looks like the below:

public interface ITrigger : IDisposable { /// <summary> /// Occurs when an input has been trigger. /// </summary> event InputTriggedEventHandler InputTrigged; /// <summary> /// Starts the trigger. /// </summary> /// <param name="trigger">The data about the trigger to start.</param> void Init(Trigger trigger); }

此接口的一种实现是 FileCreatedTrigger ,当创建文件时会触发该事件.

One implementation of this interface is a FileCreatedTrigger which fires the event when a file is created.

我想要另一个实现,可以将 Trigger 设置为以特定间隔触发(非常类似于Windows Task Scheduler).因此,我查看了 Quartz.Net ,这正是我想要的.

I want another implementation where I can set the Trigger to fire at a certain interval (much like the Windows Task Scheduler). So, I looked at the Quartz.Net and it's pretty much what I want.

问题是如何从 IJob 触发InputTrigged事件??这是Quartz所使用的.IJob仅实现execute,而execute不能调用父级(在本例中为 ITrigger ,因为它不知道这是哪个实例.

The question is How do I get the InputTrigged event to fire from an IJob? which is what Quartz uses. The IJob only implements execute which cannot call the parent (which is in this case the ITrigger as it does not know which instance this is.

希望我能说清楚.我希望能够在使用 Quartz.Net 时保留我的界面 ITrigger ,它具有另一种如何触发的实现.

Hope I made myself clear. I want to be able to keep my interface ITrigger while using Quartz.Net which has another implementation of how to trigger.

推荐答案

我最终要做的是使用guid作为键来保持对ScheduleTriggers的静态引用.然后将guid传递到jobdetail,该jobdetail使用它来查找ScheduleTrigger并引发事件.不漂亮,但能完成工作:

What I ended up doing was keeping a static reference to the ScheduleTriggers with guid as key. The guid is then passed to the jobdetail which uses it to find the ScheduleTrigger and raises the event. Not pretty, but does the job:

public class ScheduleTrigger : BaseTrigger { Guid name = Guid.NewGuid(); static Dictionary<Guid, ScheduleTrigger> triggers = new Dictionary<Guid, ScheduleTrigger>(); public static Dictionary<Guid, ScheduleTrigger> Triggers { get { return triggers; } } public void Init(Trigger triggerParam) { .... JobDetail jobDetail = new JobDetail(name.ToString(), Type.GetType(schedTrig.JobType.JobClassName)); Triggers.Add(name, this); } public void Dispose() { if (Triggers.ContainsKey(name)) { triggers.Remove(name); } base.Dispose(); } internal void RaiseEvent() { base.OnInputTrigged(string.Empty); } }

发起活动非常简单

EventRaiserJob : IJob { public void Execute(JobExecutionContext context) { Guid name = new Guid(context.JobDetail.Name); ScheduleTrigger.Triggers[name].RaiseEvent(); } }

更多推荐

Quartz.Net触发事件

本文发布于:2023-11-05 21:08:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   Quartz   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!