我正在寻找的是一个解决方案,它允许我实现适当的接口并在满足条件时触发触发器.这样的事情已经存在,还是我自己?
这是我看过的一对夫妇:
> 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类,并定义您需要的所有自定义要求,以检查该任务是否需要运行,如果需要,则调用该特定任务的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 }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。