A scheduling engine that supports more than just date / time triggers

I have found many great .NET graphics engines, especially Quartz.Net looks very promising. However, I need a planning mechanism that allows me to initiate not only dates and times, but also everything I can think of. For example, I can ask when I see that the process is started, when the computer is locked, disconnected from the WMI event, etc ... in addition to triggers based on date / time.

What I'm looking for is a solution that will allow me to implement the appropriate interface and fire the trigger when my conditions are met. Does something like this already exist or am I myself?

Here is a couple I was looking at:

This needs to be run in my .NET application. I studied a modification of Quartz.Net to support this type of launch, but the concept of date and time triggers is simply rooted; perhaps it would be easier to write my own scheduler, since I do not need to save tasks and triggers in the database.

I would prefer to disconnect from the existing scheduling system, so I don’t have to worry about implementing details such as queues, priorities, thread pools, etc .... but of course I will do what I need to do.

+4
source share
1 answer

You can declare a base class Task or an interface, depending on what you prefer by implementing the bool NeedsToRun property and the Run() method.

You can then inherit the Task class for each of your individual tasks (or use the delegate functions, task types) and define all the user requirements that you will need to check whether you need to perform this task and if it makes a call to the Run() method to this particular task.

Add all of your tasks to the List<Task> and periodically repeat them to see which task actually needs to be run, and voila; You have a very simple but effective scheduler.

Personally, I was after the priority-based scheduler, and was not event driven when you described, so I implemented Func<bool> to determine if a task and Action should be run to actually run it. My code is as follows:

 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 } 

But I believe that this can be adapted to subscribe to events and set a flag to make sure that NeedsToRun returns true when this event is fired quite easily.

+1
source

Source: https://habr.com/ru/post/1433048/


All Articles