开发者

C++ code to execute a file every hour

开发者 https://www.devze.com 2023-03-04 17:03 出处:网络
I am currently faced with a problem where I need to execute a batch file every hour, can anyone provide a code spinet that will launch a designated file every hour e.g. myfile.b开发者_开发知识库at ?Th

I am currently faced with a problem where I need to execute a batch file every hour, can anyone provide a code spinet that will launch a designated file every hour e.g. myfile.b开发者_开发知识库at ?


This isn't something you solve with an always on process. You should use a scheduled task for this. It solves all the problems for you.

If you absolutely had to do this in your own code the simplest solution is the Sleep() function. But I strongly advise against that approach.


CreateWaitableTimer, combined with any of the wait functions


Depending on your system environment you can easily accomplish this with a cronjob.

With a cronjob you can schedule it for any minute / hour / day / etc...

You can supply the cronjob to execute a path to a function where you can place a c++ exe to run the service you need.

For instance: I have a cronjob scheduled every hour to cURL a php function (this is a temporary workaround, normally i'd just run the php script) to send out emails on a user-preferece scheudle.

0 * * * * curl http://mydomain.com/my_email_scheduler.php

the 5 digits preceeding my function call determine the schedule time

[minute] [hour] [day] [month] [day of week] command-line-to-be-executed

Cron job guide

EDIT:: just noticed your comment that you don't have an available task scheduler.. I would then suggest to sleep a thread for every hour and execute a function upon waking.


An alternative to using sleep would be to use timers. It does mean you need to go through the process of creating a GUI though (probably not worth it at all if it's as simple as it sounds) <- My bad. One of the advantages of this is that you can make sure something is executed every half hour rather than every half hour + execution time for whatever it is.

0

精彩评论

暂无评论...
验证码 换一张
取 消