How to build a timer with a very low resolution?

I need to implement a Windows service that imports a database, and this once a month. The program receives data by e-mail and will import it at the end of each month.

Is there a better way than installing a sleep program for max_integer seconds / milliseconds?

+4
source share
2 answers

I would not do this as a Windows service. I would run it as a scheduled task.

Your service will just sleep for a month and just a waste of resources. Let the OS track the time and run your application for processing once a month.

+16
source

If you cannot write a window service, you will simplify your life - there may be more efficient tasks. Windows services are best used for things where there is some constant background activity that should happen - not to run tasks for a long time.

However, if you must do this with a Windows service, then you do not want to set a timer wait timeout. This is the most definitely problematic approach. What happens if your application restarts? How does he know how long he sleeps? Or what if the thread is reloaded?

The best approach is to write a record somewhere in the database that identifies when the next import should occur as a date / time. The Windows service may wake up periodically (every few minutes or hours) and see if the current date / time exceeds this value. If so, start the import and update the next launch date in the database until the next launch.

If your application is restarted, you simply read the value from the database and continue as before.

+4
source

Source: https://habr.com/ru/post/1301436/


All Articles