[Qt-interest] Qthread takes a long time to stop

Sarvesh Saran Sarvesh.Saran at synopsys.com
Sun Jun 20 10:41:16 CEST 2010


Hi,

Thanks a lot for the reply.. I will implement what you have suggested..

Also, on close, while the main GUI thread is waiting for the helper threads.., I get the following message dialog on Linux:

Warning:
The Window "sampleapp" is not responding
Force Quit the application?
Yes-no

This appears very briefly..the application then closes before the user has a chance to click on either yes or no..( probably because the wait period is over..)

Any ideas why this could be happening and what I could do to prevent it?

Thanks,
Sarvesh

From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Scott Bloom
Sent: Saturday, June 19, 2010 9:06 PM
To: Qt-Interest
Subject: Re: [Qt-interest] Qthread takes a long time to stop

You basiclly answered it yourself.

You need to shorten the getmessage and processMessage functions...

One very common technique, is inside the algorithms, query if they are allowedToRun();

Ie..

HelperThread::getmessage()
{
    if( !allowedToRun() ) return;
   step1();
    if( !allowedToRun() ) return;
    step2();
    if( !allowedToRun() ) return;
   step3();
}

And the same for processMessage()..

But at some point you will get down to a 3rd party library call, which you cant make any smaller... That will be the minimum wait time :(

Scott


On 6/19/10 8:02 AM, "Sarvesh Saran" <Sarvesh.Saran at synopsys.com> wrote:
Hi,

I have a helper thread derived from QThread which runs in a forever loop. Inside the loop it reads messages from a server , does some processing of the messages and send the message to the Main GUI thread.

So the helper thread looks something like:

Helperthread::run()
{
                while(allowedToRun())
                {
                                QString msg;
getmessage(&msg); //read a message from the server
processMessage(msg);
                }
}

Helperthread::processMessage()
{
//lots of case statements here....(to identify message types) and lots of message processing
}

//this is a public slot called by the main GUI thread
Helperthread::setAllowedToRun()
{
//set the flag to false;
}

My problem is that when the main window gets a close signal, it sets the flag to false and has to wait for a long time for the helper thread to finish.

i.e in the main window on close :

MainWindow::closeEvent()
{
helperthread->setAllowedToRun(false);
helperthread->wait();  //this takes a long time because the helper thread's processMessage is quiet big and takes a long while to return.
}

Is there a way I can speed this up so that my main window does not have to wait for a long time for the helper thread to exit ?


Thanks,
Sarvesh
________________________________
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100620/562b7d8e/attachment.html 


More information about the Qt-interest-old mailing list