I'm using a file to write out all tasks that can occur rather than a database for now. I may migrate later.
So lets say Task is a custom type with
Type TaskType ( _
LastRunTime as Long, _
NextRunTime as Long)
Now, initially (and at the end of each service run), I will run a scheduler that will set the NextRunTime for each task.
The smallest of these NextRunTime will be used in StartServiceAt("",SmallestNextRunTime)
When the service runs, it checks for which tasks need to be done:
For i = 0 to TaskList.Size -1
if DateTime.Now > Tasks.NextRunTime then
DoTask(Task(i))
End If
Next
And then before getting destroyed, it schedules the next task as above.
This sounds okay so far.
It is possible that the service will be started while it is already running a previous task. Your code should be able to cope with this.
So after completing the task, when running the scheduler for the next start, I also need to be checking which Tasks should have been run in this duration but were not run?
For this I will have to see if:
DateTime.Now > NextRunTime
and I will re-start the service straight away.
CalculateNextRunTimes
For i = 0 to TaskList.Size -1
if DateTime.Now > Tasks.NextRunTime then
'We have tasks pending
StartService("") 'Will this work with the service already active?
End If
Next
I'm not sure what happens if a service is already processing code, and it is started again? Will it start from Service_Start again? And then I have to check Service.IsRunning?
Thanks.