PDA

View Full Version : QTimer single shot and too busy app.



Pier
20th March 2007, 12:55
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.

high_flyer
20th March 2007, 13:15
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()

Pier
20th March 2007, 14:05
Another option is to use QApplication::processEvents()


I see. It works.
I inserted QApplication::processEvents() inside the while loop:


while(sqlalarm)
{
mysql_real_query(&mysql, comando.constData(), comando.size());
QCoreApplication::processEvents();
}
and it works.

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

high_flyer
20th March 2007, 15:02
It will be a better design to do it in a seperate thread.