ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-09 14:04:41  阅读:240  来源: 互联网

标签:c scheduled-tasks quartz-scheduler


我发现了许多优秀的.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,scheduled-tasks,quartz-scheduler
来源: https://codeday.me/bug/20190709/1413817.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有