Reputation: 31
I am trying to create a program that runs certain functions every hour in mfc c++.
I couldn't find any good tutorial outside on how to do this as I am still in the learning process and trying to expand my knowledge on this.
Just a little something so it wouldn't seem that I am just asking empty questions, so far I created a program that reads value from an .INI file and stored it in the global variables within the program. I've manage to make it run indefinitely in the System Tray and my plan is to ultimately parse some text from a .txt file into a .csv file every hour.
So to reiterate, can anyone explain to me the logic and a rough method on how to do it? For my learning process, i would just like to know how i can show
MessageBox("Hello");
every 1 minute or so just for my own understanding.
Upvotes: 1
Views: 1188
Reputation: 24887
Use a Windows timer. Get the wall-time and calculate how many ms are left until the next XX:00. Set the timer interval to, (say), half that. In the timer handler, repeat that calcuation and timer-set until the time remaining is less than 1000 ms and then set the timer to the remaining interval. Next time it fires, perform your operation and then set the timer for the next hour.
You can 'creep up' on the hour quite accurately in such a manner.
Upvotes: 0
Reputation: 234795
As you're using MFC
then I'm supposing you're using Windows as your OS.
The normal way of approaching this is to use the Task Scheduler in Windows to schedule the running of your program at the desired intervals. (You can use the Task Scheduler to configure command line arguments too).
I'd be reluctant to use timers in the application itself as I'd question the stability of such an approach.
Upvotes: 3