Results 1 to 6 of 6

Thread: Execute a funcion at an exact time of day

  1. #1
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Execute a funcion at an exact time of day

    I have this code:

    Qt Code:
    1. QTimer *timer=new QTimer;
    2. connect(timer, SIGNAL(timeout()),this,SLOT(delete_old_tuples()));
    3. timer->start(3600000);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::delete_old_tuples(){
    2. QDateTime now = QDateTime::currentDateTime();
    3. QDateTime timeoftheaction (QDate (now.date()), QTime (15,0,0));
    4. if (now >= timeoftheaction){
    5. QMessageBox::critical(0, qApp->tr("Deleting tuples"),
    6. qApp->tr("Click Cancel to exit."), QMessageBox::Cancel);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Is this code correct? I want to delete the tuples of my database everyday at 15:00....Is there a better way?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Execute a funcion at an exact time of day

    Quote Originally Posted by KillGabio View Post
    Is this code correct?
    No

    Is there a better way?
    Yes. Setup a cron action (or whatever your platform's equivalent is).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    KillGabio (28th May 2013)

  4. #3
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Execute a funcion at an exact time of day

    Windows Task Scheduler should be. But SO annoying to learn.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  6. The following user says thank you to ChrisW67 for this useful post:

    KillGabio (28th May 2013)

  7. #5
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Execute a funcion at an exact time of day

    Quote Originally Posted by KillGabio View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. how to execute multiple commands at a time
    By PHANI in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2012, 09:01
  2. Replies: 2
    Last Post: 5th October 2010, 08:20
  3. get exact height of a QString in pixels
    By zack in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2010, 13:16
  4. Execute an action on regular time
    By valy12 in forum Qt Programming
    Replies: 2
    Last Post: 21st May 2010, 10:25
  5. exact region of drawn part
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 18:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.