Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Schedule function calls?

Status
Not open for further replies.

ThankGodItsFriday

Programmer
May 12, 2005
11
SE
I have an arraylist of objects where I need to run an execution function for each diffrent object at a diffrent time.

Sofar I've been leaning towards solving it with a timer that iterates through the objects and checks if System.DateTime.Now() > obj.ExecutionDate
It feels however like sloppy programming since the majority of timer events won't be used.

Is there a way to shedule the function call for each object?

 
I wouldn't exactly call that sloppy unless your array is huge and your interval is set to very small values. If that is an option then you could also add a timer to each of your objects (again, a matter of how many they are!) and set the individual interval to (obj.ExecutionDate -System.DateTime.Now() - <some safety>) and go from there. Your individual timers would only fire once at the actual ExecutionDate.

Volker/
 
Probably they will be neither, so mayby I shouldn't bother with it to much. I'll check out your suggestion in any case.

Thnx!
 
An alternative is to iterate through your array, to get the time of the one that occurs soonest. Set ONE timer for this time, and wait for it. As there is no guarantee that your process will actually get to execute immediately the timer pops, you should iterate through the array and call any functions that have passed their execution times. If some of the functions may be long-running, you might want to restart the loop in case any more have passed their sell-by date while you were processing. Once you are satisfied that there are no items currently waiting to execute, issue the timer and start again.

With this scheme, you don't have to manage multiple timers, and neither does the system. You aren't polling uselessly every n milliseconds to discover that you don't have anything to do for the next 10 minutes, either.

But don't forget to have an event that you can use to tell the thread to cancel the timer and exit...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top