我正在尝试杀死/取消 QThread。我遵循了https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/的实施
我像这样创建 QThread:
workerThread = new QThread();
ImageProcessor* worker = new ImageProcessor();
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread->start();
并尝试通过调用在某个 UI 事件上停止它:
workerThread->quit();
我的工人看起来像这样:
class ImageProcessor : public QObject
{
Q_OBJECT
public:
explicit ImageProcessor(QObject *parent = 0);
~ImageProcessor();
signals:
void finished();
public slots:
void process();
private:
//...
};
进程调用具有大量计算的 API
void ImageProcessor::process()
{
// API call...
emit finished();
}
我的问题是,在线程上调用退出后,线程继续运行,即 API 的计算不会停止。因为工作人员只调用 API,所以我无法在工作人员循环中使用取消检查。有任何想法吗?
提前致谢!