Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gui freeezing when using threading

I am stuck and was hoping someone could help me.

I have made a class/gui with a loading bar set to marquee so that when a task is being carried out i could display it to the user.

In one of my gui classes, in the constructor on the first line i am making a new instance of this class and then doing

    LoadingBar bar = new LoadingBar();
    Thread thread = new Thread(bar.Show);
    thread.Start();

However, even tho the main programme thread is going off doing some more intensive stuff, this gui still seems to freeze, even if i use backround worker.

Is there anything wrong with the approach i have mentioned and if so what do i need to change?

Thanks

like image 516
rik Avatar asked Mar 04 '11 14:03

rik


People also ask

Why use QThread?

Using QThread to Prevent Freezing GUIs. A common use for threads in a GUI application is to offload long-running tasks to worker threads so that the GUI remains responsive to the user's interactions. In PyQt, you use QThread to create and manage worker threads.

How do I stop freezing tkinter?

A very simple workaround is spawning a long running process onto a separate thread. This will still be able to communicate with Tkinter and update it's GUI (for the most part).

Is pyqt5 thread safe?

While some parts of the Qt framework are thread safe, much of it is not. The Qt C++ documentation provides a good overview of which classes are reentrant (can be used to instantiate objects in multiple threads).

How do you stop QThread in Python?

QThread will notify you via a signal when the thread is started() and finished() , or you can use isFinished() and isRunning() to query the state of the thread. You can stop the thread by calling exit() or quit() . In extreme cases, you may want to forcibly terminate() an executing thread.


1 Answers

You need to reverse your method. The GUI needs to stay in the main thread while the work is done in a "worker thread" (typically a BackGroundWorker). Then the worker reports back to the GUI which then updates.

like image 65
Vincent Vancalbergh Avatar answered Oct 03 '22 15:10

Vincent Vancalbergh