Click here to Skip to main content
Click here to Skip to main content

Schedule Different Tasks in your Apps

, 18 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Set up a schedule of tasks to be performed - programmatically or via a GUI

Introduction

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 Smile | :) ).
  • 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.

Screenshot - TaskScheduler4.png

The 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.

The 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 (EStatus).

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.

Finally the 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 EditScheduling dialog-box.

Screenshot - TaskScheduler2.jpg

Screenshot - TaskScheduler3.jpg

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 EditScheduling dialog-box.

The sample application (as seen on the below figure) can insert a small demo schedule:

Screenshot - TaskScheduler1.jpg

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.

License

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.

History

  • 7th September, 2007 - Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jesper Olsen
Engineer
Denmark Denmark
No Biography provided

Comments and Discussions

 
QuestionMessage Automatically Removed PinmemberSai Pranad12-May-14 10:18 
GeneralHelp on passing back the Task Name to the Consoletxtbox PinmemberWilliam Dugdale19-Aug-09 12:00 
GeneralRe: Help on passing back the Task Name to the Consoletxtbox PinmemberRavi5R43-Mar-10 20:40 
GeneralUnhandled Exception when Adding a new Schedulation, when no Scheduled tasks are listed PinmemberWilliam Dugdale19-Aug-09 11:57 
GeneralAt Login with empty password crashes PinmemberTom Willett7-Mar-08 12:20 
QuestionRe: At Login with empty password crashes PinmemberJesper Olsen8-Mar-08 0:09 
GeneralRe: At Login with empty password crashes PinmemberTom Willett10-Mar-08 4:39 
GeneralUnable to open the project Pinmemberpavanpranesh11-Oct-07 6:04 
GeneralRe: Unable to open the project PinmemberJ.Olsen11-Oct-07 10:17 
QuestionWhere to define Tasks PinmemberHemant.Kamalakar28-Sep-07 20:56 
AnswerRe: Where to define Tasks PinmemberJ.Olsen29-Sep-07 8:15 
Questionschedulations? Pinmemberrobvon24-Sep-07 13:20 
GeneralTimer/Thread Sleep Alternative PinmemberMike Doyon18-Sep-07 4:00 
AnswerRe: Timer/Thread Sleep Alternative [modified] PinmemberJ.Olsen18-Sep-07 9:42 
GeneralRe: Timer/Thread Sleep Alternative PinmemberBoneSoft24-Sep-07 10:26 
AnswerRe: Timer/Thread Sleep Alternative PinmemberJ.Olsen24-Sep-07 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 18 Sep 2007
Article Copyright 2007 by Jesper Olsen
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid