Click here to Skip to main content
15,888,802 members
Articles / Programming Languages / Visual Basic
Article

A Basic Scheduler

Rate me:
Please Sign up or sign in to vote.
2.64/5 (5 votes)
28 May 2008CPOL2 min read 46.7K   1.4K   15   11
Define schedules to the program for executing programmer defined jobs

Introduction

I needed a simple scheduler to execute on an exact time / time span for generating automated reports. I checked various articles and the Internet for a scheduler that could tick off exactly as required and if it failed, then at the nearest next time.

Background

While searching the Internet, I found an article by Andy Brummer which provided me with quite a few ideas to go about the code, but I needed something much more simpler, so it did not solve my purpose. Moreover, I was programming in VB.NET and the supplied codes were in C#, which on the whole would make it difficult for me to implement.

Before we continue any further, it would be a great help if you could read about delegates, calling and storing. I would suggest that you read a good article by Abhishek Khanna on delegates. It is a very important addition in VB.NET and simplifies many issues.

I would also suggest that you go through the Microsoft Library - MSDN about delegates, it's a very resourceful guide for quite a few programming issues.

Logic

The entire code block is wrapped within a class named Scheduler. It is dependant on three objects, namely:

  1. System.Windows.Forms.Timer
  2. System.Collections.ArrayList
  3. System.Data.DataTable

The DataTable is used for storing all events happening in the duration. This is done so as to allow immediate changes in the values. I had tried using ArrayList and other collection objects, but I did not get proper results doing so.

VB.NET
SetSchedule.Columns.Add(New DataColumn("SchType", System.Type.GetType("System.String")))
SetSchedule.Columns.Add(New DataColumn
    ("exeTime", System.Type.GetType("System.DateTime")))
SetSchedule.Columns.Add(New DataColumn("active", System.Type.GetType("System.Boolean")))
SetSchedule.Columns.Add(New DataColumn
    ("NextexeTime", System.Type.GetType("System.DateTime")))
SetSchedule.Columns.Add(New DataColumn
    ("TimeDiff", System.Type.GetType("System.TimeSpan")))

I added these five fields to the DataTable for keeping a track of the schedule, and I enumerated the following:

VB.NET
Public Enum Scheduled
    Once = 1
    BySecond = 2
    ByMinute = 4
    ByHour = 8
    Daily = 16
    Weekly = 32
    Monthly = 64
    Yearly = 128
    Regular = 256
End Enum

I used an ArrayList to store all events to execute on the scheduler match.

Using the Code

Add the class project to your solution and take a reference to it in your project.

Declare a variable as follows:

VB.NET
Dim sch As New Scheduler.Scheduler

Declare and allocate your delegates to functions and Subs:

VB.NET
'Declare a Delegate without Parameters
Delegate Sub ShowMessage()
'Declare a Delegate With Parameters
Delegate Sub ShowMessageBox(ByVal str As String)

'Define a Sub/Function without Parameters
Private Sub DisplayMessage()
    RichTextBox1.Text += "It is time: " + Now.ToString + vbCrLf
End Sub

'Define a Sub/Function with Parameters
Private Sub DisplayMessageBox(ByVal str As String)
    MsgBox("It is time: " + Now.ToString)
End Sub

'Pass a Delegate without Parameters
Dim msd As ShowMessage = AddressOf DisplayMessage

'Pass a Delegate with Parameters
Dim msd1 As ShowMessageBox = AddressOf DisplayMessageBox

Set the scheduler timers:

VB.NET
'Add a Schedule to Do the Events On a Particular Date/Time
sch.AddSchedule(Now)
'Add a Schedule to do the Events, after every Time Span
sch.AddSchedule(New TimeSpan(0, 5, 0))
'Add a Schedule to do the Events, after every Time Span starting from Date/Time
sch.AddSchedule(#5/28/2008 4:00:00 AM#, New TimeSpan(0, 5, 0))
'Add a Schedule to do the Events, EveryDay at the Said Times in 24Hrs Clock
sch.AddSchedule("09:25", "21:25")

Declare the events:

VB.NET
sch.AddEvent(msd)
sch.AddEvent(msd1, "Test String")

Start the scheduler:

VB.NET
sch.Start()

History

  • 28th May, 2008: First release

Contact Me

In case you have any problems with the code, you can contact me at dfdosabhai@gmail.com or on my IM (Instant Messenger) dfdosabhai@yahoo.com

License

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


Written By
Database Developer Wizard Enterprise Pvt Ltd
India India
Database Developer

Comments and Discussions

 
QuestionA couple more examples? Pin
beckerben10-Oct-08 17:57
beckerben10-Oct-08 17:57 
Generalin practice use whats built in to windows or linux Pin
Leblanc Meneses2-Jun-08 8:35
Leblanc Meneses2-Jun-08 8:35 
GeneralRe: in practice use whats built in to windows or linux Pin
Noctris24-Jul-08 6:20
Noctris24-Jul-08 6:20 
GeneralRe: in practice use whats built in to windows or linux Pin
Leblanc Meneses24-Jul-08 8:30
Leblanc Meneses24-Jul-08 8:30 
GeneralRe: in practice use whats built in to windows or linux Pin
Noctris8-Oct-08 0:27
Noctris8-Oct-08 0:27 
GeneralRe: in practice use whats built in to windows or linux Pin
Leblanc Meneses8-Oct-08 10:10
Leblanc Meneses8-Oct-08 10:10 
> For example: i need an opening message to play once at 9am
> i also need several audio ads to play throughout the day, every 15
> minutes it needs to pick one from a list
> i need another certain promotion to play hourly BUT, only in the
> afternoon..

> And this is just one customer, i can image other customers wanting
> different things..

so my question is .. why would you want to handle this all yourself?

I would create 4 scheduled tasks
1) customerrequirements.playopeningmessage
2) customerrequirements.random15.morning.at_0_15_30_45
3) customerrequirements.random15.afternoon.at_15_30_45
4) customerrequirements.promo.afternoon.at_0


each task will call the appropriate exe with its parameters.

customerrequirements.exe switch=playopeningmessage
customerrequirements.exe switch=random15
customerrequirements.exe switch=promo

please explore the scheduled task.. you'll see my setup can easily implement your requirements and doesn't require you to add an extra service mimicking what you already get for free!


the app is now simple.
switch(args["switch"])
{
case "playopeningmessage": break;
case "random15": break;
case "promo": break;
default: throw new NotImplementedException();
}


the only place your article will make sense is:
You purchased a microcontroller setup an interupt on the PIT, periodic interrupt timer, and needed to manually create
what we get for free on all major operating systems the scheduled task or cron
GeneralDatabase enabled.... Pin
Stewart Roberts30-May-08 7:14
Stewart Roberts30-May-08 7:14 
GeneralNice work Pin
Ashutosh Phoujdar30-May-08 3:34
Ashutosh Phoujdar30-May-08 3:34 
GeneralLets state the obvious [modified] Pin
Programm3r28-May-08 19:50
Programm3r28-May-08 19:50 
GeneralTypo in title Pin
leppie28-May-08 6:49
leppie28-May-08 6:49 

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.