Results 1 to 4 of 4

Thread: QTimer single shot and too busy app.

  1. #1
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTimer single shot and too busy app.

    Hi to all!
    Just a question on timers.

    Is it possible that an application becomes so busy that a QTimer never triggers?
    An example is:

    ---------------------------------
    ...
    sqlalarm = true;
    QTimer::singleShot(3000, this, SLOT(change_alarm()));

    while(sqlalarm)
    {
    mysql_real_query(&mysql, comando.constData(), comando.size());
    }
    ...
    ---------------------------------
    ---------------------------------
    ...
    void PDialog::change_alarm()
    {
    sqlalarm = false;
    }
    ...
    ---------------------------------

    My suspect is that the query it performs on the mysql database (it is an INSERT statement, just to see how many inserts can be done in a fixed amount of time; in this case 3 secs.) takes too much CPU and this is the reason for which the QTimer doesn't trigger.
    Consider that I'm pretty newby to QT, so probably I'm writing a stupid consideration.
    Anyway the problem persists and I'm not able to stop inserting records in the database.

    Any help is very welcome.
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTimer single shot and too busy app.

    In cases where you have a "busy endless loop" it is better to you use an extra thread.
    Another option is to use QApplication::processEvents()

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

    Pier (20th March 2007)

  4. #3
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer single shot and too busy app.

    Another option is to use QApplication:rocessEvents()
    I see. It works.
    I inserted QApplication:rocessEvents() inside the while loop:

    Qt Code:
    1. while(sqlalarm)
    2. {
    3. mysql_real_query(&mysql, comando.constData(), comando.size());
    4. QCoreApplication::processEvents();
    5. }
    To copy to clipboard, switch view to plain text mode 
    and it works.

    Which is the best approach in your opinion? This with the processEvents or creating an extra thread?

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTimer single shot and too busy app.

    It will be a better design to do it in a seperate thread.

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

    Pier (20th March 2007)

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.