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(); } }
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.
source share