Click here to Skip to main content
6,821,293 members and growing! (18,129 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

A Basic Scheduler

By Didara F Dosabhai

Define schedules to the program for executing programmer defined jobs
VB6, VB (VB7.x, VB8.0, VB9.0), .NET (.NET1.0, .NET1.1, .NET2.0, .NET3.0, .NET3.5)VS2005, VS2008, Design
Posted:28 May 2008
Views:8,986
Bookmarked:9 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.84 Rating: 2.64 out of 5
1 vote, 20.0%
1
2 votes, 40.0%
2

3

4
2 votes, 40.0%
5

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.

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 delegates to functions and Subs:

    '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:

'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:

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

License

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

About the Author

Didara F Dosabhai


Member
Database Developer
Occupation: Database Developer
Company: Wizard Enterprise Pvt Ltd
Location: India India

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
QuestionA couple more examples? Pinmemberbeckerben18:57 10 Oct '08  
Generalin practice use whats built in to windows or linux PinmemberLeblanc Meneses9:35 2 Jun '08  
GeneralRe: in practice use whats built in to windows or linux PinmemberNoctris7:20 24 Jul '08  
GeneralRe: in practice use whats built in to windows or linux PinmemberLeblanc Meneses9:30 24 Jul '08  
GeneralRe: in practice use whats built in to windows or linux PinmemberNoctris1:27 8 Oct '08  
GeneralRe: in practice use whats built in to windows or linux PinmemberLeblanc Meneses11:10 8 Oct '08  
GeneralDatabase enabled.... PinmemberStewart Roberts8:14 30 May '08  
GeneralNice work Pinmemberdnpro4:34 30 May '08  
GeneralLets state the obvious [modified] Pinmember Programm3r 20:50 28 May '08  
GeneralTypo in title Pinmember leppie 7:49 28 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 28 May 2008
Editor: Deeksha Shenoy
Copyright 2008 by Didara F Dosabhai
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project