-1

I have made a program in c++ for changing the password of a system and I wanna run it for every 2 hours,then I end up with two choice in c++ ,one is Sleep(ms) and the other is using recent thread lib this_thread::sleep_for(2h)[ 2h using std::chrono_literals].

The doubt I have been wandering is, does long pausing an exe will work the way we want, is it any other better way than what i mentioned?

I have also planned to put my exe as a windows service.

RaGa__M
  • 2,550
  • 1
  • 23
  • 44

2 Answers2

2

any other better way than what i mentioned?

Yes.

I suggest, that you do not pause the program at. Simply do the thing, and exit.

Extract the scheduling part to a separate program. You don't even need to write this scheduler, because it already exists on most operating systems.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • user2079303,No scheduled task, as I am changing the user password by overriding some of the user info I prefer service because it run as a system process and I hope it has high privilege than what it run as just task – RaGa__M Jul 26 '16 at 11:42
2

If you have some task that must be run periodically with long periods of waiting, you should use a program or script, that does the task and exits, and a scheduler, which handles the waiting. There're also questions you need to consider, for example:

  • do you need to start your task if the scheduled time was missed (due to reboot, for example)
  • do you allow several of your tasks to run at once, if time it takes to complete is longer than wait period

What you're trying to do is to implement a scheduler yourself. If this is what you want, then sleep is a posix function, and chrono::thread::sleep_for is cross-platform, so it's better to use the second one.

However, it's not generally recommended to implement schedulers, moreover, so simple ones.

buld0zzr
  • 962
  • 5
  • 10