Execute a funcion at an exact time of day
I have this code:
Code:
connect(timer, SIGNAL(timeout()),this,SLOT(delete_old_tuples()));
timer->start(3600000);
Code:
void MainWindow::delete_old_tuples(){
if (now >= timeoftheaction){
qApp
->tr
("Click Cancel to exit."),
QMessageBox::Cancel);
}
}
Is this code correct? I want to delete the tuples of my database everyday at 15:00....Is there a better way?
Re: Execute a funcion at an exact time of day
Quote:
Originally Posted by
KillGabio
Is this code correct?
No
Quote:
Is there a better way?
Yes. Setup a cron action (or whatever your platform's equivalent is).
Re: Execute a funcion at an exact time of day
Windows Task Scheduler should be. But SO annoying to learn.
Re: Execute a funcion at an exact time of day
If you really don't want to use system features specifically built for the task then do it yourself.
Have your program wake up few seconds (or minute, whatever), look at the current time, and if it is within the timer period after the prescribed time have it do something. Then ensure that, no matter what, your program is always running (e.g. as a service or with a service acting as watchdog...).
Your current code only look once an hour to see if work needs to be done so it was never going to be "exactly" 15:00, and it would execute once an hour between 15:00 and midnight.
Re: Execute a funcion at an exact time of day
Yes I realized that. But after doing some research the delete of the rows can be done anytime between 15 and 17 so that gives me plenty of time to do it with the code posted. Lucky me! Thank you all :)
Re: Execute a funcion at an exact time of day
Quote:
Originally Posted by
KillGabio
Yes I realized that. But after doing some research the delete of the rows can be done anytime between 15 and 17 so that gives me plenty of time to do it with the code posted. Lucky me! Thank you all :)
The problem is, with you current code the delete can happen many times during one day.