更新时间:2023-11-14 14:23:58
您当然可以在每次迭代检查的Worker::run()
中的for循环中放入一个布尔值,该值在== true处中断,由gui Stop设置.按钮.当然,这在执行被阻止时不会退出线程.
You could of course put a boolean value within the for loop in Worker::run()
checked every iteration, which breaks on ==true and is set by the gui Stop button. Of course, this won't quit the thread while execution is blocked.
可能更好的方法是摆脱for
循环,并使用Qt的信号和插槽来设置回调函数,该回调函数连接到类似于QIODevice::readyRead()
的信号.仅当套接字/管道中有可用信息时,才调用它们.在任何其他时间,您都可以使用QThread::exit()
退出线程.您还需要在某个时刻调用QThread::exec()
,以使事件循环继续进行.
Probably better is to get rid of the for
loop and use Qt's signals and slots to setup a callback function, connected to a signal like QIODevice::readyRead()
. These will be called only when there is information available in the socket/pipe whatever. Any other time you'll be able to quit the thread with QThread::exit()
. You'll need to call QThread::exec()
at some point as well to get the event loop going.