<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">I believe you're misunderstanding what "wait" does.<div><br>First of all, your loop will keep going since you're not telling it to stop in any way: as the name says, wait() will only *wait* until the thread associated with the QThread object has finished execution (as explained in the documentation [1]).</div><div>Your thread will keep running until the condition of the while loop is met (the counter has reached at least 101). Since the loop is time based, it will exit only after the required amount of time.</div><div>Also, wait() is *blocking*, meaning that it won't return until the thread has actually finished (causing the UI to freeze in the meantime). This is obviously not what you're looking for.</div><div><br>If you have a continuous loop (a loop that cycles periodically at small time intervals), you can use a simple flag:</div><div><br></div><div><div>    def long_running(self):</div><div>        self.counter = 0</div><div>        self.keepRunning = True</div><div>        while self.counter <= 100 and self.keepRunning:</div></div><div>            # ...</div><div><br></div><div>And then set that flag whenever you want to stop the thread:</div><div><br></div><div><div>    def WaitThread(self):</div><div>        self.obj.keepRunning = False</div></div><div><br></div><div>This is obviously the more simple way to do so, if the thread itself has some blocking you need some timeout (for example, using a Queue with a proper argument for get()) or an appropriate Queue result management in order to exit the loop as soon as possible.</div><div><br>I *strongly* suggest you to carefully read the post from Giuseppe (and the following thread) from your previous question, including all the provided links, as I'm under the impression that you're still misunderstanding some important aspects about multithreading (and I can understand that, as it's a subject that often creates confusion).</div><div><br></div><div>That said, as a general suggestion, you should *not* name functions/methods with starting uppercase letters. Only classes and constants use that styling, and for a very good reason: it allows to clearly tell apart classes/constants from variables/attributes, which is a very important aspect in code readability.</div><div><br></div><div>Maurizio</div><div><br></div><div>[1] - <a href="https://doc.qt.io/qt-5/qthread.html#wait">https://doc.qt.io/qt-5/qthread.html#wait</a></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno gio 1 lug 2021 alle ore 18:46 Demosthenes Koptsis <<a href="mailto:demosthenesk@gmail.com">demosthenesk@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello, i test Qthread and i made an example with a Form, Run, Stop <br>
buttons and a progressbar.<br>
<br>
I want to click Run button and start a thread and to click Stop button <br>
and Wait the thread.<br>
<br>
I try to implement a movetothread example but the wait fails.<br>
<br>
Here is the code.<br>
<br>
MainWindow.py<br>
<br>
-------------------------<br>
<br>
# -*- coding: utf-8 -*-<br>
<br>
# Form implementation generated from reading ui file 'MainWindow.ui'<br>
#<br>
# Created by: PyQt5 UI code generator 5.15.4<br>
#<br>
# WARNING: Any manual changes made to this file will be lost when pyuic5 is<br>
# run again.  Do not edit this file unless you know what you are doing.<br>
<br>
<br>
from PyQt5 import QtCore, QtGui, QtWidgets<br>
<br>
<br>
class Ui_Form(object):<br>
     def setupUi(self, Form):<br>
         Form.setObjectName("Form")<br>
         Form.resize(400, 138)<br>
         self.btnRun = QtWidgets.QPushButton(Form)<br>
         self.btnRun.setGeometry(QtCore.QRect(20, 20, 89, 25))<br>
         self.btnRun.setObjectName("btnRun")<br>
         self.btnStop = QtWidgets.QPushButton(Form)<br>
         self.btnStop.setGeometry(QtCore.QRect(20, 60, 89, 25))<br>
         self.btnStop.setObjectName("btnStop")<br>
         self.progressBar = QtWidgets.QProgressBar(Form)<br>
         self.progressBar.setGeometry(QtCore.QRect(20, 100, 371, 23))<br>
         self.progressBar.setProperty("value", 24)<br>
         self.progressBar.setObjectName("progressBar")<br>
         self.dial = QtWidgets.QDial(Form)<br>
         self.dial.setGeometry(QtCore.QRect(200, 20, 50, 64))<br>
         self.dial.setObjectName("dial")<br>
         self.lcdNumber = QtWidgets.QLCDNumber(Form)<br>
         self.lcdNumber.setGeometry(QtCore.QRect(260, 20, 64, 71))<br>
         self.lcdNumber.setObjectName("lcdNumber")<br>
<br>
         self.retranslateUi(Form)<br>
         QtCore.QMetaObject.connectSlotsByName(Form)<br>
<br>
     def retranslateUi(self, Form):<br>
         _translate = QtCore.QCoreApplication.translate<br>
         Form.setWindowTitle(_translate("Form", "Test Thread"))<br>
         self.btnRun.setText(_translate("Form", "Run"))<br>
         self.btnStop.setText(_translate("Form", "Stop"))<br>
<br>
<br>
----------------------------------------<br>
<br>
<br>
MoveToThread.py<br>
<br>
----------------------------------------<br>
<br>
from PyQt5.QtWidgets import *<br>
from PyQt5.QtCore import *<br>
from MainWindow import *<br>
import sys<br>
import time<br>
<br>
class SomeObject(QObject):<br>
     finished = pyqtSignal()<br>
     changed = pyqtSignal(int)<br>
<br>
     def __init__(self, parent=None, counter_start=0):<br>
         super(SomeObject, self).__init__(parent)<br>
         self.counter = counter_start<br>
<br>
     def long_running(self):<br>
         self.counter = 0<br>
         while self.counter <= 100:<br>
             time.sleep(0.3)<br>
             self.counter += 1<br>
             self.changed.emit(self.counter)<br>
             print(str(self.counter))<br>
         self.finished.emit()<br>
<br>
class MainWindow(QWidget):<br>
     def __init__(self, parent=None):<br>
         super(MainWindow, self).__init__(parent)<br>
         self.ui = Ui_Form()<br>
         self.ui.setupUi(self)<br>
         self.center()<br>
         #Init progressBar<br>
         self.ui.progressBar.setValue(0)<br>
         #Buttons<br>
         self.ui.btnRun.clicked.connect(self.StartThread)<br>
         self.ui.btnStop.clicked.connect(self.WaitThread)<br>
         self.ui.dial.sliderMoved.connect(self.SetLCD)<br>
         #Init Thread<br>
         self.objThread = QThread()<br>
         self.obj = SomeObject()<br>
         self.obj.moveToThread(self.objThread)<br>
         self.obj.finished.connect(self.objThread.quit)<br>
         self.obj.changed.connect(self.SetProgressBarValue)<br>
         self.objThread.started.connect(self.obj.long_running)<br>
<br>
     def SetLCD(self):<br>
         self.ui.lcdNumber.display(self.ui.dial.value())<br>
<br>
     def WaitThread(self):<br>
         self.objThread.wait()<br>
<br>
     def StartThread(self):<br>
         self.objThread.start()<br>
<br>
     def SetProgressBarValue(self):<br>
         self.ui.progressBar.setValue(self.obj.counter)<br>
<br>
     def center(self):<br>
         # geometry of the main window<br>
         qr = self.frameGeometry()<br>
<br>
         # center point of screen<br>
         cp = QDesktopWidget().availableGeometry().center()<br>
<br>
         # move rectangle's center point to screen's center point<br>
         qr.moveCenter(cp)<br>
<br>
         # top left of rectangle becomes top left of window centering it<br>
         self.move(qr.topLeft())<br>
<br>
if __name__ == '__main__':<br>
     app = QApplication(sys.argv)<br>
     w = MainWindow()<br>
     #   Disable maximize window button<br>
     w.setWindowFlags(Qt.WindowCloseButtonHint | <br>
Qt.WindowMinimizeButtonHint)<br>
     w.show()<br>
     sys.exit(app.exec_())<br>
----------------------------------------------<br>
<br>
When i call WaitThread the emit of counter stops but the while loop <br>
works after all<br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>