Best way to shut down program after certain time
-
27-06-2021 - |
Pregunta
I am working with a console application that scans multiple image directories and re-sizes then saves images if they exceed a certain size. Once a directory is finished processing a log is then pushed to a web-service page.
This utility program will be run at night for a given number of hours but must be shut down before normal office hours the following day, regardless of if it finished processing the current folder.
I am aware of 3 options to shut the program down:
- Via Scheduled task setting "shut down after X hours": The concern I have with this option is that it might potentially close the program in the middle of the process, such as between saving the image and logging to the server.
- After processing each image check the
DateTime
and if after certain time close it - User a
Timer
, set tick to a few hours, once tick handler is called then set a bool flag to false. After processing each image check the bool flag to see if it is time to shut the program down.
Does the timer option have a performance advantage over comparing DateTime?
Is there perhaps an even better way? Maybe I am missing something?
Solución
Checking the DateTime
between processing seems like the best approach here.
Even if you use a Timer
, you'll likely still want to check the current time when the timer event fires to make sure that it's time to exit. Just checking the time against a cutoff time seems like the simplest option all around.