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

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Sat Jun 19 20:22:57 CEST 2010


Have a look at the mandelbrot example:
http://doc.trolltech.com/4.6/threads-mandelbrot.html

Here in the inner loop, where the calculations happen, it checks for
certain flags (restart/abort) to see if the main GUIK thread has set
it while it was scheduled out.

Probably you can do something similar in your long processMessage() fxn?

HTH,
-mandeep

On Sat, Jun 19, 2010 at 8:32 PM, 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
>
>




More information about the Qt-interest-old mailing list