J
Jevon
I have previously looked into this a bit, and came up with something along
the lines of the following (I've read the rest of the thread up to this
point, and you still seem stuck on the idea of a loop and/or lots of
threads. I don't know how your tasks are stored, but have you thought about:
Upon initialisation and/or startup and/or addition/change to the list of
tasks:
Cancel current timer if not startup.
Re-order tasks by next run time.
Do tick code.
Do tick code:
Check for tasks that should already have run (now > next run time) and run
them if necessary - i.e. if there's been a power failure.
Update the next run time for the task, and insert it into the list in
the correct location (i.e. so you don't mess up the ordering).
Find the task with the next soonest run time and set a timer to run once and
fire at that time which calls Do tick code.
You will need to check what happens if you set a timer with a negative
value - this could happen with tasks that are sheduled to start very close
together - you'd check for tasks that should have run, find the next time,
and by the time the timer has been set up, that time has already passed. In
this instance, the program might never fire the tick code again, and
effectively stall.
With this way, you only need to loop through as many tasks that should have
already run but haven't yet - you stop when you get to the first task with a
"next run time" > now.
Does that make sense? This was written very quickly, so might have a mistake
or two
Jevon
the lines of the following (I've read the rest of the thread up to this
point, and you still seem stuck on the idea of a loop and/or lots of
threads. I don't know how your tasks are stored, but have you thought about:
Upon initialisation and/or startup and/or addition/change to the list of
tasks:
Cancel current timer if not startup.
Re-order tasks by next run time.
Do tick code.
Do tick code:
Check for tasks that should already have run (now > next run time) and run
them if necessary - i.e. if there's been a power failure.
Update the next run time for the task, and insert it into the list in
the correct location (i.e. so you don't mess up the ordering).
Find the task with the next soonest run time and set a timer to run once and
fire at that time which calls Do tick code.
You will need to check what happens if you set a timer with a negative
value - this could happen with tasks that are sheduled to start very close
together - you'd check for tasks that should have run, find the next time,
and by the time the timer has been set up, that time has already passed. In
this instance, the program might never fire the tick code again, and
effectively stall.
With this way, you only need to loop through as many tasks that should have
already run but haven't yet - you stop when you get to the first task with a
"next run time" > now.
Does that make sense? This was written very quickly, so might have a mistake
or two

Jevon