Skip to content

Instantly share code, notes, and snippets.

@Pymmdrza
Created December 20, 2024 14:59
Show Gist options
  • Save Pymmdrza/f21bbfb8bce0a06e513cc7c3b0e8dcf4 to your computer and use it in GitHub Desktop.
Save Pymmdrza/f21bbfb8bce0a06e513cc7c3b0e8dcf4 to your computer and use it in GitHub Desktop.
PyQt5 Sleep Thread on threading Process (progressBar)
# github.com/Pymmdrza
# source method use on project PyQt5
import ctypes
import win32con
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QProgressBar, QPushButton
from win32process import SuspendThread, ResumeThread
class Worker(QThread):
valueChanged = pyqtSignal(int)
handle = -1
def run(self):
try:
self.handle = ctypes.windll.kernel32.OpenThread( # @UndefinedVariable
win32con.PROCESS_ALL_ACCESS, False, int(QThread.currentThreadId()))
except Exception as e:
print('Get Thread Error: ', e)
print('thread id', int(QThread.currentThreadId()))
for i in range(1, 101):
print('Thread Value: ', i)
self.valueChanged.emit(i) # noqa
QThread.sleep(1)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
self.progressBar = QProgressBar(self)
self.progressBar.setRange(0, 100)
layout.addWidget(self.progressBar)
self.startButton = QPushButton('Start', self, clicked=self.onStart) # noqa
layout.addWidget(self.startButton)
self.suspendButton = QPushButton('Suspend', self, clicked=self.onSuspendThread, enabled=False) # noqa
layout.addWidget(self.suspendButton)
self.resumeButton = QPushButton('Resume', self, clicked=self.onResumeThread, enabled=False) # noqa
layout.addWidget(self.resumeButton)
self.stopButton = QPushButton('Stop', self, clicked=self.onStopThread, enabled=False) # noqa
layout.addWidget(self.stopButton)
print('main id', int(QThread.currentThreadId()))
self._thread = Worker(self)
self._thread.finished.connect(self._thread.deleteLater)
self._thread.valueChanged.connect(self.progressBar.setValue) # noqa
def onStart(self):
print('Main ID', int(QThread.currentThreadId()))
self._thread.start() # Start the thread
self.startButton.setEnabled(False)
self.suspendButton.setEnabled(True)
self.stopButton.setEnabled(True)
def onSuspendThread(self):
if self._thread.handle == -1:
return print('handle is wrong')
ret = SuspendThread(self._thread.handle)
print('Thread Suspend: ', self._thread.handle, ret)
self.suspendButton.setEnabled(False)
self.resumeButton.setEnabled(True)
def onResumeThread(self):
if self._thread.handle == -1:
return print('handle is wrong')
ret = ResumeThread(self._thread.handle)
print('Thread Resume: ', self._thread.handle, ret)
self.suspendButton.setEnabled(True)
self.resumeButton.setEnabled(False)
def onStopThread(self):
self.startButton.setEnabled(True)
self.suspendButton.setEnabled(False)
self.resumeButton.setEnabled(False)
ret = ctypes.windll.kernel32.TerminateThread( # @UndefinedVariable
self._thread.handle, 0)
print('Thread Stop: ', self._thread.handle, ret)
self.stopButton.setEnabled(False)
def closeEvent(self, event):
if self._thread.isRunning():
self._thread.quit()
del self._thread
super(Window, self).closeEvent(event)
if __name__ == '__main__':
import sys
import os
print('PID', os.getpid())
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
@Pymmdrza
Copy link
Author

Screen_Record_PyQt5_Thread_Sleep_Process

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment