Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

Schedule Different Tasks in your Apps

Rate me:
Please Sign up or sign in to vote.
4.50/5 (13 votes)
18 Sep 2007CPOL4 min read 81.2K   4.7K   91   16
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 :-) ).
  • 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)


Written By
Engineer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHelp on passing back the Task Name to the Consoletxtbox Pin
William Dugdale19-Aug-09 11:00
William Dugdale19-Aug-09 11:00 
GeneralRe: Help on passing back the Task Name to the Consoletxtbox Pin
Ravi5R43-Mar-10 19:40
Ravi5R43-Mar-10 19:40 
GeneralUnhandled Exception when Adding a new Schedulation, when no Scheduled tasks are listed Pin
William Dugdale19-Aug-09 10:57
William Dugdale19-Aug-09 10:57 
GeneralAt Login with empty password crashes Pin
Tom Willett7-Mar-08 11:20
Tom Willett7-Mar-08 11:20 
QuestionRe: At Login with empty password crashes Pin
Jesper Olsen7-Mar-08 23:09
Jesper Olsen7-Mar-08 23:09 
GeneralRe: At Login with empty password crashes Pin
Tom Willett10-Mar-08 3:39
Tom Willett10-Mar-08 3:39 
GeneralUnable to open the project Pin
pavanpranesh11-Oct-07 5:04
pavanpranesh11-Oct-07 5:04 
GeneralRe: Unable to open the project Pin
Jesper Olsen11-Oct-07 9:17
Jesper Olsen11-Oct-07 9:17 
QuestionWhere to define Tasks Pin
Hemant.Kamalakar28-Sep-07 19:56
Hemant.Kamalakar28-Sep-07 19:56 
AnswerRe: Where to define Tasks Pin
Jesper Olsen29-Sep-07 7:15
Jesper Olsen29-Sep-07 7:15 
Questionschedulations? Pin
robvon24-Sep-07 12:20
robvon24-Sep-07 12:20 
GeneralTimer/Thread Sleep Alternative Pin
Mike Doyon18-Sep-07 3:00
Mike Doyon18-Sep-07 3:00 
AnswerRe: Timer/Thread Sleep Alternative [modified] Pin
Jesper Olsen18-Sep-07 8:42
Jesper Olsen18-Sep-07 8:42 
GeneralRe: Timer/Thread Sleep Alternative Pin
BoneSoft24-Sep-07 9:26
BoneSoft24-Sep-07 9:26 
AnswerRe: Timer/Thread Sleep Alternative Pin
Jesper Olsen24-Sep-07 18:45
Jesper Olsen24-Sep-07 18:45 

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

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