Aluode/PerceptionLabPortable
0
1from .Qt import QtCore2 3__all__ = ['ThreadsafeTimer']4 5class ThreadsafeTimer(QtCore.QObject):6 """7 Thread-safe replacement for QTimer.8 """9 10 timeout = QtCore.Signal()11 sigTimerStopRequested = QtCore.Signal()12 sigTimerStartRequested = QtCore.Signal(object)13 14 def __init__(self):15 QtCore.QObject.__init__(self)16 self.timer = QtCore.QTimer()17 self.timer.timeout.connect(self.timerFinished)18 self.timer.moveToThread(QtCore.QCoreApplication.instance().thread())19 self.moveToThread(QtCore.QCoreApplication.instance().thread())20 self.sigTimerStopRequested.connect(self.stop, QtCore.Qt.ConnectionType.QueuedConnection)21 self.sigTimerStartRequested.connect(self.start, QtCore.Qt.ConnectionType.QueuedConnection)22 23 24 def start(self, timeout):25 isGuiThread = QtCore.QThread.currentThread() == QtCore.QCoreApplication.instance().thread()26 if isGuiThread:27 #print "start timer", self, "from gui thread"28 self.timer.start(int(timeout))29 else:30 #print "start timer", self, "from remote thread"31 self.sigTimerStartRequested.emit(timeout)32 33 def stop(self):34 isGuiThread = QtCore.QThread.currentThread() == QtCore.QCoreApplication.instance().thread()35 if isGuiThread:36 #print "stop timer", self, "from gui thread"37 self.timer.stop()38 else:39 #print "stop timer", self, "from remote thread"40 self.sigTimerStopRequested.emit()41 42 def timerFinished(self):43 self.timeout.emit()44 