[Interest] QEventLoop -- not sure I am correctly using this

Jason Kretzer Jason at gocodigo.com
Mon Dec 2 20:27:59 CET 2013


Maybe that last bit would be a bit more clear.

The first qDebug statement writes out — I have it set to give the time when it is written as well — but the qDebug does not write until after the heavy task is completed in backgroundtaskmanager.  Any other time, this performs as expected, ticking along every second.

-Jason
- - - - - - - - - - - - - - - - - - - - - - -
Jason R. Kretzer

Application Developer
Google # 606-887-9011
Cell # 606-792-0079
Jason at gocodigo.com<mailto:Jason at gocodigo.com>

“quidquid latine dictum sit altum videtur”


[cid:image001.png at 01CECA4D.CA4030A0]

On Dec 2, 2013, at 2:14 PM, Jason Kretzer <Jason at gocodigo.com<mailto:Jason at gocodigo.com>> wrote:

Good Day,

I have in my application, two classes.  When the application starts up, one of the classes -backgroundtaskmanager - gets instantiated.  In the constructor, a QTimer is started to timeout every 5 minutes and call runTasks.  The instantiation is then moved to a separate thread so that it can run without impacting the rest of the application.

Here are the contents of the constructor.

QTimer* timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(runTasks()));
    timer->start(300000); //fiveminutes
    thread = new QThread();
    this->moveToThread(thread);
    thread->start();


The second class -player- is instantiated in the main thread.  Once it is instantiated, it begins doing other tasks.  Basically, a method is called with the following code.  This code is designed to make the while loop delay for one second before executing the rest of the loop (one second-ish, I know it is not perfect but it has good enough resolution for what I need).


while(!taskQueue.isEmpty()) {
        qDebug() << “BEFORE QEVENTLOOP”;
QEventLoop wait1Sec;
        QTimer::singleShot(1000, &wait1Sec,SLOT(quit()));
        wait1Sec.exec();
aDebug() << “AFTER QEVENTLOOP”;

QList<Task*> instances;
 for(int t=seconds; t>previousTime; t—) {
  QList<Task*> vals = taskQueue.values(t);
 while(!vals.isEmpty()) {
 instances.insert(0, vals.takeLast());
 }
 taskQueue.remove(t);
 }
 previousTime = seconds;
 miliSeconds = QDateTime::currentMSecsSinceEpoch() - today.toMSecsSinceEpoch();
 seconds = miliSeconds/1000;
}

NOTE:  the two sets of tasks these do are not related.

So, anyway, every 5 minutes the tasks in backgroundtaskmanager are run.  Some of these tasks can take up to a minute.  When the backgroundtaskmanager class runs a task that does take this long (remember it is in a separate thread), the player class code above gets hung in what appears to be the inside of the QEventLoop.  The qDebug statements don’t write out until the tasks in the backgroundtaskmanager are finished.  Then it rushes through to catch up.

My question is, am I correctly doing this at all?  Is this the proper use of a QEventLoop?

Thanks!

-Jason

- - - - - - - - - - - - - - - - - - - - - - -
Jason R. Kretzer

Application Developer
Google # 606-887-9011
Cell # 606-792-0079
Jason at gocodigo.com<mailto:Jason at gocodigo.com>

“quidquid latine dictum sit altum videtur”


<image001.png>

_______________________________________________
Interest mailing list
Interest at qt-project.org<mailto:Interest at qt-project.org>
http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131202/ea2c0bdb/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 13452 bytes
Desc: image001.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131202/ea2c0bdb/attachment.png>


More information about the Interest mailing list