Qt5: How to create a (cleanup) task that runs once per day at 3:00am?

315 views Asked by At

I am using Qt5 under Windows7.
I know how to create a task using QThread, but my problem is:
How do I run it every day at 03:00AM?
I was thinking about QTimer, but it doesn't seem to be ok... it can't be linked somehow to 03:00am.

Just to make it clear: I can't use some Windows application(s). It must be coded inside my Qt app as it does some cleaning job too: cleanup history list, trim it down to 1000 lines (or whatever), etc. So, you see I can't do that using TaskScheduler or similar Windows tools...

3

There are 3 answers

2
Bowdzone On

Whats wrong with using a QTimer? I agree that a task scheduler is the better option. Here, only about 0,03% of the time code is executed it is really supposed to do something. If the exact moment is not as important you can increase the timer interval and the check-boundaries and reduce the unncessary calls. But if you prefer such a solution this should work:

someclass::someclass(){

    member_timer = new QTimer(this);
    QObject::connect(member_timer, SIGNAL(timeout()), this, SLOT(check_time()));
    member_timer->start(30000);

    member_cleanup_performed = false;
}

void someclass::check_time(){

    QTimer ctime = QTime::currentTime();
    if(ctime.hour() == 3 && ctime.minute() == 0){

        if(member_cleanup_performed == false){
            this->cleanup();
            member_cleanup_performed = true;
        }
    }else{
        member_cleanup_performed = false;
    }
}
2
Proxytype On

you can use windows task scheduler to do this for you

enter image description here

1
Frank Osterfeld On

If you can use C++11, have a look at std::this_thread::sleep_until. Run it in a separate thread and let the thread emit a signal connected to a slot in the main thread, which then performs the action. That of course requires that your application is actually running at 3 am.