c# – 支持更多日期/时间触发器的调度引擎
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 支持更多日期/时间触发器的调度引擎,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3062字,纯文字阅读大概需要5分钟。
内容图文
我发现了许多优秀的.NET调度引擎,特别是Quartz.Net看起来非常有前景.但是,我需要一个调度引擎,它不仅可以触发日期和时间,还可以触发我能想到的任何东西.例如,除了基于日期/时间的触发器之外,我可能希望在我看到进程已启动,计算机被锁定,脱离WMI事件等时触发.
我正在寻找的是一个解决方案,它允许我实现适当的接口并在满足条件时触发触发器.这样的事情已经存在,还是我自己?
这是我看过的一对夫妇:
> What is the best way to represent “Recurring Events” in database?
> c# recurring event (like for a calendar)
> Recommend a C# Task Scheduling Library
> How to write C# Scheduler
> http://www.codeproject.com/Articles/2478/C-Scheduler
> http://www.codeproject.com/Articles/2407/A-New-Task-Scheduler-Class-Library-for-NET
> http://www.codeproject.com/Articles/8424/A-component-for-event-scheduling-inside-an-applica
> http://blog.bobcravens.com/2009/10/an-event-based-cron-scheduled-job-in-c/
> http://www.codeproject.com/Articles/6507/NET-Scheduled-Timer
这需要在我的.NET应用程序中运行.我考虑修改Quartz.Net以支持这种类型的触发,但是日期/时间触发器的概念只是根深蒂固;编写自己的调度程序可能会更容易,因为我不需要将作业和触发器保存到数据库.
我更喜欢使用现有的调度系统,所以我不必担心实现队列,优先级,线程池等等细节……但当然我会做我必须做的事情.
解决方法:
您可以声明一个基本的Task类或接口,无论您喜欢哪个实现bool属性NeedsToRun和方法Run().
然后,您可以为每个单独的任务(或使用委托函数,任务类型)继承Task类,并定义您需要的所有自定义要求,以检查该任务是否需要运行,如果需要,则调用该特定任务的Run()方法.
将所有任务添加到列表<任务>并定期迭代它们以查看实际需要运行的任务,并且瞧;你有一个非常简单但有效的调度程序.
就个人而言,我正在使用基于优先级的调度程序而不是您描述的事件驱动调度程序,因此我实现了一个Func< bool>确定是否需要运行任务以及实际运行它的Action.我的代码如下:
public class Task : IComparable<Task>
{
public Task(int priority, Action action, Func<bool> needsToRun, string name = "Basic Task")
{
Priority = priority;
Name = name;
Action = action;
_needsToRun = needsToRun;
}
public string Name { get; set; }
public int Priority { get; set; }
private readonly Func<bool> _needsToRun;
public bool NeedsToRun { get { return _needsToRun.Invoke(); } }
/// <summary>
/// Gets or sets the action this task performs.
/// </summary>
/// <value>
/// The action.
/// </value>
public Action Action { get; set; }
public void Run()
{
if (Action != null)
Action.Invoke();
}
#region Implementation of IComparable<in State>
/// <summary>
/// Compares the current object with another object of the same type.
/// </summary>
/// <returns>
/// A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other"/> parameter.Zero This object is equal to <paramref name="other"/>. Greater than zero This object is greater than <paramref name="other"/>.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public int CompareTo(Task other)
{
return Priority == other.Priority && Name == other.Name ? 1 : 0;
}
#endregion
}
但我认为这可以适应订阅事件并设置一个标志,以确保每当该事件被轻易触发时NeedsToRun返回true.
内容总结
以上是互联网集市为您收集整理的c# – 支持更多日期/时间触发器的调度引擎全部内容,希望文章能够帮你解决c# – 支持更多日期/时间触发器的调度引擎所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。