polandeer
polandeer

Reputation: 396

How do I stop a QThread from Closing?

I'm writing a launcher for Minecraft (see full code here: http://pastie.org/6633420), which I know is a game, but it's still fun to program for.

At the moment, I am having issues with threading with QThread because all the threads seem to close before they should. I'm getting the error: QThread: Destroyed while thread is still running. Is there any way to fix this besides doing what I did earlier in the code which was

for i in range(1,50):
    QThread.msleep(200)
    QCoreApplication.processEvents()

because I'm sure it really bad practice and does not work well, as it causes the application to be unresponsive at some points.

Upvotes: 2

Views: 947

Answers (1)

D K
D K

Reputation: 5760

This is actually a sort of bug in the Python bindings for Qt. QThreads instantiated inside of a function (MainWidget.update_software, in your case) are garbage collected when the function returns, which causes the execution to end early with the QThread: Destroyed while thread is still running message.

To solve this problem, there are two options:

  1. Call .wait() on a thread inside a function so the thread can finish processing before the function returns. This will block the main thread however, which is probably not what you want.
  2. Make all thread instances either instance variables or globals so that the lifetime of the thread lasts the duration of the contained instance or program, respectively.

Upvotes: 7

Related Questions