Execute a task every 12 hours

Hello everyone, I need to execute a function every 12 hours, the function is inside a C++ application.

After reading some post/goolge, I think settimer and Signal is a possible choice.
I'd like the CPU was as idle as possible between these 12 hours.

Is there a better way to do it?.

Thanks and regards


Hi, rossig

You'll need to know some Windows functions. The best choice is Sleep(). :thumbs up

Sleep(1000) means the program will sleep exactly 1 second.

Each hour has 3600 seconds, and your function time is 12 hours.
And the value : 1000 (a second) * 3600 (a hour) * 12 (interval) = 43200000 (milliseconds)

Use the 'long' variable definition.
Last edited on
Rather than having the program doing nothing, then waking up twice a day, write a seperate program that perforns the task you're interested in and schedule it to run twice a day.

1. You won't have to worry if it's running or not.
2. It won't consume system resources until it needs to collect stats.
3. You can "upgrade" your program more easily in a production environment.
4. You don't need to worry about resource leaks in a long running program.
5. It's the standard thing to do.
Hi, thanks for your responses. I apologize I didn't mention I was developing under Linux (Ubuntu 12.04).

As for kbw suggested, I already have the program. The question is what I need to do to schedule it to run.

Another issue I have to deal with is that this period may be variable. For instance, tomorrow it begins at 3AM but next week at 5AM. I'd like to automatize it if it's possible.

But a scheduled task on Linux it would be a good start.


Last edited on
You should use cron to schedule the tasks. You'll need to adjust the config (crontab) when the schedule changes.
thanks kbw, I was reading about it, but it has a problem if I need a binary with parameters it won't work, me thinks.

The rusty solution I found is to have a text file that could be read by the binary/script in order to get these parameters
In fact I need to read several registers from a MySQL table, but as I understood I can set binary files with cron/crontab as well.

I appreciate any suggestion anyway.
Thanks a lot, regards

Last edited on
Use cron to start a script that knows how to run your steps. It's a standard pattern to wrap such things in scripts that take simple text parameters if necessary.

The general principle is to use standard patterns to reduce complexity.
Topic archived. No new replies allowed.