Pause & Resume QThread

Mohamed Aziz Bessrour

I'm trying to Pause a QThread and resume it.

So, i have an RFID Reading Loop in the QThread and i want to pause the endless loop when the reader gets an RFID Code. After that, there is a db checking. At the end of the check, i want to resume the RFID Reading Loop to get other codes.

MVCE:

def main():
    global Thread
    app = QtGui.QApplication(sys.argv)
    main = Main()
    Thread = RFID_Thread()
    Thread.rfid_event.connect(Main().on_event)
    Thread.start()
    sys.exit(app.exec_())

class Main(object):
    def __init__(self):
        self.accueil = MainWindow(self)
        self.access = AccessWindow()
        self.accueil.show()

    def on_event(self, data):
        # I WANT TO PAUSE THE QTHREAD HERE

        ###################################
        #   CHECKING DB & SHOWING UI      #
        ###################################

        # AND RESUME IT HERE
class RFID_Thread(QtCore.QThread):
    rfid_event = pyqtSignal(str, name='rfid_event')
    def run(self):
        while 1:
            ser = serial.Serial(port=Serial_Port, baudrate=Serial_Baudrate)
            a = ser.read(19).encode('hex')
            ser.close()
            if len(a) <> 0:
                Code = a[14:]
                self.rfid_event.emit(Code)
                time.sleep(2)

if __name__=='__main__':
    main()

the code can't be reproduced because you need an RFID Reader but we can simulate him by this two lines instead of opening serial port and reading data from it:

a = "**************e20030654408021520403f4b"
time.sleep(4)

I tried to use a status variable but it don't works.

Mohamed Aziz Bessrour

Finally i've resolved the problem by my self

MVCE:

def main():
    global Thread
    app = QtGui.QApplication(sys.argv)
    main = Main()
    Thread = RFID_Thread()
    Thread.rfid_event.connect(Main().on_event)
    Thread.start()
    sys.exit(app.exec_())

class Main(object):
    def __init__(self):
        self.accueil = MainWindow(self)
        self.access = AccessWindow()
        self.accueil.show()

    def on_event(self, data):
        # I WANT TO PAUSE THE QTHREAD HERE

        Thread.Pause = False
        ###################################
        #   CHECKING DB & SHOWING UI      #
        ###################################

        # AND RESUME IT HERE
        Thread.Pause = True
class RFID_Thread(QtCore.QThread):
    rfid_event = pyqtSignal(str, name='rfid_event')
    Pause = True
    def run(self):
        while 1:
            if Pause:
                  ser = serial.Serial(port=Serial_Port, baudrate=Serial_Baudrate)
                  a = ser.read(19).encode('hex')
                  ser.close()
                  if len(a) <> 0:
                         Code = a[14:]
                         self.rfid_event.emit(Code)
                         time.sleep(2)
            else:
                  continue
 if __name__=='__main__':
    main()

Finally, the State variable was the solution !!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related