How to pause and resume threads in pyqt?

Hi @SinPool welcome the forum.

For the pause/resume functionality you need to add a flag onto the thread, which can be set/unset from outside. In the __init__ of your thread class, add a flag, e.g. self.is_paused = False.

In the main loop, check for the state of this flag, and perform an loop while waiting. You will want to add a time.sleep() in there to avoid hogging the cpu doing nothing, set the time depending on how quickly you need it to start up when unpaused.

while self.is_paused:
     time.sleep(0.1)

To pause the thread from outside, you just need to set the is_paused flag to True. You can either do this directly, or using methods, e.g.

def pause(self):
    self.is_paused = True

def resume(self):
    self.is_paused = False

Then on your thread object you can call

thread.pause()
# ...
thread.resume()

To pause and restart it.

1 Like