更新时间:2022-12-28 17:35:53
我认为你可以做以下两件事之一:
I would think you could do one of two things:
我可能会使用一个事件,以便您知道线程正在休眠,并且您不会在线程写入值时尝试读取它们.
I would probably use an event so that you know that the thread is sleeping and you aren't trying to read values while the thread is writing them.
在您的 ExampleApp
类中,您需要存储事件并处理超时:
In your ExampleApp
class, you would need to store the event and handle the timeouts:
class ExampleApp(QtGui.QMainWindow, masimo.Ui_MainWindow):
def __init__(self, event, parent=None):
super(self.__class__, self).__init__()
self.setupUi(self)
self.dataWasReadEvent = event
self.checkThreadTimer = QtCore.QTimer(self)
self.checkThreadTimer.setInterval(500) #.5 seconds
self.checkThreadTimer.timeout.connect(self.readListValues)
def readListValues(self):
if self.dataWasReadEvent.is_set():
#Read your events from the list and update your fields
self.dataWasReadEvent.clear() #Clear the event set flag so that nothing happens the next time the timer times out
您的 SerialRead
函数需要采用线程事件的参数,并且需要在串行读取之后但在重新启动之前设置该事件:
Your SerialRead
function would need to take an argument that is the threading event and the event would need to be set after the serial read but before the restart:
def SerialRead(dataReadEvent):
...
dataReadEvent.set()
threading.Timer(restart, SerialRead, args=(dataReadEvent,)).start()
您的 main
函数还需要接受要传递给 ExampleApp 的初始化程序的事件参数:
Your main
function will also need to accept an event argument to be passed to the initializer for ExampleApp:
def main(dataReadEvent):
...
form = ExampleApp(dataReadEvent)
最后,在您的 if __name__ == '__main__':
部分,需要创建线程事件并将其传递给 Thread 调用:
And finally, in your if __name__ == '__main__':
section, the threading event would need to be created and passed to the Thread calls:
if __name__ == '__main__':
dataReadEvent = threading.Event()
Thread(target = SerialRead, args=(dataReadEvent,) ).start()
Thread(target = main, args=(dataReadEvent,) ).start()