I need to write a windows service which performs the same operations every X minutes.
What is the best way to achieve this, with respect to memory management (e.g. allowing the GC to clean up)
1) while(true) loop, with my own logic to ensure the operation only occurs every X minutes. I am worried this will block the GC from cleaning up however.
2) Timer, with some logic to make sure if the operation takes longer than the timer interval it does start performing it twice at the same time (more complicated that 1)
3) Other suggestions?
In both (1) and (2) there is no reason to worry about GC.
(1) is okay if my own logic doesn’t boil down to busy loop (which would use CPU for no reason). The easiest way to implement that logic is to
Sleepfor X minutes.(2) is preferable if it’s important for the task to begin regularly: e.g. given the interval of 15 minutes and the start at 0:00, the next task should begin at 0:15, the next at 0:30, and so on, not “discounting” the time elapsed while doing the task.