Did you ever want to schedule different tasks in your apps or give the user the possibility to do so? With this program, you can do it and it has a nice little GUI too.
You basically start by setting up different tasks you want to perform (done in code) and then via code or the GUI, you program the schedule.
This will enable you to let the user of your application decide when different tasks should be performed and how often. E.g. you have a peer2peer manager that you only want to run at times when you are normally not using your Internet connection, so you setup a schedule to start the manager at 12 pm and then stop the manager at, say, 8 am every day or you have a database that needs backup every workday at 2 am, you can do this by setting up 5 schedulations that repeat on a weekly basis.
What Do We Need?
We should be able to setup different tasks that are to be performed and then setup the schedule specifying when we want the tasks performed. A schedulation is one instance of a task that needs to be performed.
It should be possible to setup the tasks/schedule entirely through code, making it possible for other programmers to make their own GUI for editing the schedule, but there should also be a default GUI that the user/programmer can easily use.
How To Do It
- We need a list of all the tasks that can be executed (This could easily be implemented by using my commandprocessor. I did program this scheduler before I made the commandprocessor so that's why it's not used here. But using this could be an exercise for the reader ).
- A list of all the schedulations.
- A way to edit schedulations (GUI and add/remove methods in code).
- A timer that every-so-often checks the schedule and see if any schedulations should be executed.
Below is a diagram of the important classes and enumerations used.
CTask class represents a task that can be executed and it MUST have a unique name. This name is used to display for the user in the GUI so he can choose the right task for the job.
CSchedulation class represents a schedulation and it must have a
starttime when it should be activated and a
CTask to perform.
It also has properties to tell how often it should be executed (
EOcurrence) and a status telling if it has been deactivated (
If the schedulation's
OcurrenceType is set to other that "
Once", the Occurrence start/stop time steps into effect and defines a window in time where this schedulation CAN be executed. If the current time is outside this window, the schedulation is deactivated.
CScheduler class is where everything is happening. It has methods for adding tasks and schedulations, starting and stopping the scheduler and a method for showing the GUI for editing the schedule.
When you start the scheduler, it creates a thread where it every x'th-millisecond checks the schedule to see if any tasks should be executed.
All tasks are executed asynchron by using .NET's threadpool. This means that you don't have to worry about using too much time in the tasks.
Using the Code
First you fill in tasks that can be performed and finally you fill in the scheduling of tasks either by doing it in code or by letting the user do it through the
At some time, you need to call the
StartScheduling method to start the scheduling. This you can do at any time you please. You don't necessarily have to do it after you add tasks and schedules – it can be done at any time.
As you can see in the edit-dialogbox, active schedulations are in green and inactive ones are in red and if a schedulation's occurrence window is outside the current time, the entry gets a yellow background. If the schedulation is a "
Once"-type the occurrence start/stop are greyed because they are then not used.
If this is to be used solely for the benefit of the developer, then you don't need to use the
AddNewScheduledTask method as this method is only used for the
The sample application (as seen on the below figure) can insert a small demo schedule:
You can also edit the schedule and start the scheduling.
Every time a task is executed, it writes a message in the output textbox.
Improvements That Can Be Made
- Use the commandProcessor.
- Synchronization is needed between the scheduling thread and the methods to manipulate the list of schedulations.
- Colors on the GUI only changes when the user presses a button. A thread could check to see if the time changes so occurrence windows are outside the current time.
This is freeware though I am still the copyright holder of this software.
If you use this software in your own programs, you must give me credit where credit is due and if you use the GUI-part of this software you are not allowed to remove the About-button and the associated About-dialogbox.
- 7th September, 2007 - Version 1.0