加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 支持更多日期/时间触发器的调度引擎

发布时间:2020-12-15 21:40:02 所属栏目:百科 来源:网络整理
导读:我发现了许多优秀的.NET调度引擎,特别是Quartz.Net看起来非常有前景.但是,我需要一个调度引擎,它不仅可以触发日期和时间,还可以触发我能想到的任何东西.例如,除了基于日期/时间的触发器之外,我可能希望在我看到进程已启动,计算机被锁定,脱离WMI事件等时触发.
我发现了许多优秀的.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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读