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 delegate
s, calling and storing. I would suggest that you read a good article by Abhishek Khanna on delegate
s. 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:
System.Windows.Forms.Timer
System.Collections.ArrayList
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.
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:
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:
Dim sch As New Scheduler.Scheduler
Declare and allocate your delegate
s to functions and Sub
s:
Delegate Sub ShowMessage()
Delegate Sub ShowMessageBox(ByVal str As String)
Private Sub DisplayMessage()
RichTextBox1.Text += "It is time: " + Now.ToString + vbCrLf
End Sub
Private Sub DisplayMessageBox(ByVal str As String)
MsgBox("It is time: " + Now.ToString)
End Sub
Dim msd As ShowMessage = AddressOf DisplayMessage
Dim msd1 As ShowMessageBox = AddressOf DisplayMessageBox
Set the scheduler
timers:
sch.AddSchedule(Now)
sch.AddSchedule(New TimeSpan(0, 5, 0))
sch.AddSchedule(#5/28/2008 4:00:00 AM#, New TimeSpan(0, 5, 0))
sch.AddSchedule("09:25", "21:25")
Declare the event
s:
sch.AddEvent(msd)
sch.AddEvent(msd1, "Test String")
Start
the scheduler
:
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