Scheduled tasks

  • Thread starter Thread starter AxOn
  • Start date Start date
A

AxOn

Hi,

Does anybody know how to get an application do something at a specific
time or date? I want to be able to choose when to back up some files,
for example once a month, once a week or every hour.

Thanks!
 
Hi,

Does anybody know how to get an application do something at a specific
time or date? I want to be able to choose when to back up some files,
for example once a month, once a week or every hour.

If your app is going to run infrequently and doesn't need to be aware of
state, I would use Windows Schedulerto handle scheduling:

http://www.codeproject.com/csharp/tsnewlib.asp

Otherwise you can use the timer classes.
 
AxOn,

Here is some sample code I got awhile back which may help:

Create a class to hold the information about each job:

Public Class Job
Public Name As String
Public ScheduledDT As DateTime
Public Msg As String
End Class

Store instances of Job to represent each event in an ArrayList.
Then you pass this ArrayList to scheduleJobs() which will add each event to
a HashTable and create a Timer for it:

Code:

Public Class Form1
Inherits System.Windows.Forms.Form

Private Jobs As New Hashtable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' build some test jobs to be scheduled (you would build these from
your XML files)
Dim j As Job
Dim jal As New ArrayList

j = New Job
j.Name = "Event1"
j.Msg = "Wake up!"
j.ScheduledDT = DateTime.Now.AddSeconds(30)
jal.Add(j)

j = New Job
j.Name = "Event2"
j.Msg = "Reset Alarm Clock"
j.ScheduledDT = DateTime.Now.AddSeconds(31)
jal.Add(j)

j = New Job
j.Name = "Event3"
j.Msg = "Reset Alarm Clock Again"
j.ScheduledDT = DateTime.Now.AddSeconds(45)
jal.Add(j)

j = New Job
j.Name = "Event4"
j.Msg = "Crawl out of bed..."
j.ScheduledDT = DateTime.Now.AddSeconds(60)
jal.Add(j)

scheduleJobs(jal)
End Sub

Private Sub scheduleJobs(ByVal JobAL As ArrayList)
Dim j As Job
Dim t As System.Timers.Timer
Dim ms As Double

For Each j In JobAL
ms = j.ScheduledDT.Subtract(DateTime.Now()).TotalMilliseconds
If ms > 0 Then ' if the event time has not yet passed...
t = New System.Timers.Timer(ms)
AddHandler t.Elapsed, AddressOf Me.t_Elapsed
Jobs.Add(t, j)
t.AutoReset = False ' fire it only once
t.Start()
End If
Next
Debug.WriteLine("---------------------")
Debug.WriteLine(Jobs.Count & " Job(s) Scheduled")
End Sub

Private Sub t_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
Dim t As System.Timers.Timer = CType(sender, System.Timers.Timer)
Dim j As Job = CType(Jobs.Item(t), Job)

' do something with the job
Debug.WriteLine("---------------------")
Debug.WriteLine(j.Name)
Debug.WriteLine(j.ScheduledDT)
Debug.WriteLine(j.Msg)

' remove the job from the hashtable and destroy the timer
Jobs.Remove(t)
t.Dispose()

Debug.WriteLine("---------------------")
Debug.WriteLine(Jobs.Count & " Job(s) Left")
End Sub
End Class


Hope this helps,

Steve
 
Back
Top