This is the code which is triggered every 10 minutes. (it is triggered by a scheduled task)
The page which calls the code runs the StartTask sub on the Sceduler class, this spins off a Delegate thread which proceeds to load the tasks and run the items which are due to run. The tasks all execute without a drama, so i'm suspicious of how the process finishes which is at the end of ExecuteQueuedTasks. Should i be doing something to terminate the thread? I also wonder if it is the 'Shared' nature of the _TaskQueue...
Can anyone shed some light on this?
CODE
Public Class Scheduler
Public Shared _TaskQueue As New ArrayList()
Public Sub StartTask()
Try
TaskWorker.BeginInvoke(Nothing, Nothing)
Catch ex As Exception
Dim Message As New OurSettingsTableAdapters.MessagesTableAdapter
Message.Insert("Task Error", 1, ex.Message, False, Now.AddHours(Classes.TimeOffset), "Task Error", 0, 0, 0, 0)
Message.Dispose()
End Try
End Sub
Private Delegate Sub GetXMLDelegate()
Dim TaskWorker As New GetXMLDelegate(AddressOf ExecuteQueuedTasks)
Public Sub LoadTasks()
Dim Adaptor As New OurTasksTableAdapters.TasksTableAdapter
Dim Table As New OurTasks.TasksDataTable
Adaptor.FillNextTasks(Table, Now.AddHours(Classes.TimeOffset))
If Table.Rows.Count = 0 Then
Return
End If
For Each row As OurTasks.TasksRow In Table
Dim Task As New Task
Task.Load(row.TaskID, row.Task, row.Frequency, row.RunNext, row.Priority, row.AccountID, row.Comments)
_TaskQueue.Add(Task)
Next row
End Sub
Public Sub ExecuteQueuedTasks()
Dim Tasks As New ArrayList()
If _TaskQueue.Count = 0 Then
LoadTasks()
End If
' Collect which jobs are overdue
For Each Task As Task In _TaskQueue
If Task.RunNext <= DateTime.Now.AddHours(xtraparking.Classes.TimeOffset) Then
Tasks.Add(Task)
End If
Next Task
' Execute the jobs that are overdue
For Each Task As Task In Tasks
SyncLock _TaskQueue
Task.Execute()
_TaskQueue.Remove(Task)
End SyncLock
Next Task
End Sub
End Class