如何在QThread上停止长时间操作?



我做了一些研究,但我找不到答案,为什么我的解决方案不工作。但我要强调一点。

我已经得到了QWidget作为一个单独的对话框在我的应用程序。我使用这个QWidget类来收集路径到文件和压缩到一个文件与ZipLib。为了给用户一些关于压缩进度的反馈,我添加了QProgressBar,它在压缩过程中更新。但我发现,在某些情况下,当文件太大时,压缩文件需要很长时间,这使得我的应用程序在这段时间内没有响应。所以我的想法是移动压缩的长操作到另一个线程使用QThread,一切都很好,压缩工作,进度条更新,但有一个问题,当我想取消压缩操作。在我目前的方法中,不要听我的任何请求来打断线程,即使我关闭了对话框,线程也在做压缩。我将向你们展示我的方法也许有一些非常琐碎的东西,但我就是无法取消这个压缩操作。

tl;博士版:我不能用requestInterruption()或任何其他方式中断QThread。

这是我的WorkerThread类:
class WorkerThread : public QThread
{
Q_OBJECT
public:
void setup(std::string zip, std::vector<std::string> file)
{
mZip = zip;
mFiles = file;
mActionStopped = false;
}
void run() override {
size_t progressValue = (100 / mFiles.size());
try
{
for (const auto& file : mFiles)
{
if (not isInterruptionRequested())
{
Q_EMIT updateTheProgress(progressValue);
ZipFile::AddFile(mZip, file);
}
else
{
return;
}
}
}
catch(const std::exception& e)
{
Q_EMIT resultReady(false);
}
if (not mActionStopped)
{
Q_EMIT updateTheProgress(100 - actualProgress); // To set 100%
Q_EMIT resultReady(true);
}
}
std::string mZip;
std::vector<std::string> mFiles;
size_t actualProgress = 0;
bool mActionStopped;
Q_SIGNALS:
void resultReady(bool dupa);
void updateProgress(int value);
public Q_SLOTS:
void updateTheProgress(int value)
{
actualProgress += value;
Q_EMIT updateProgress(actualProgress);
}
void stopWork()
{
mActionStopped = true;
}
};

在我的QWidget类中,我有这样的内容:

workerThread = new WorkerThread();
connect(workerThread, &WorkerThread::resultReady, this, &ZipProjectDialog::zipDone);
connect(workerThread, SIGNAL(updateProgress(int)), progressBar, SLOT(setValue(int)));
connect(btnBox, &QDialogButtonBox::rejected, workerThread, &WorkerThread::requestInterruption);
connect(btnBox, &QDialogButtonBox::rejected, workerThread, &WorkerThread::stopWork);
workerThread->setup(zipFilename, filePathList);
workerThread->start();
connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);

我已经遵循了QThread文档,但requestInterruption()仍然不工作。

如果有人有办法解决这个问题,我将不胜感激。

谢谢!

不能中断

的执行
ZipFile::AddFile(mZip, file);

本身。这是对一个函数的一个单独调用,没有人在该函数中检查isInterruptionRequested()

一个简化的例子

#include <QCoreApplication>
#include <QDebug>
#include <QThread>
#include <QTimer>
class MyThread : public QThread
{
public:
void run() override
{
qDebug() << "starting";
for (int i = 0; i < 10; ++i) {
qDebug() << "loop" << i;
if (!isInterruptionRequested())
sleep(5);
else
break;
}
qDebug() << "finished";
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread thread;
QTimer::singleShot(0, &thread, [&thread] {
thread.start();
QTimer::singleShot(7000 /*7 seconds*/, &thread, [&thread] {
qDebug() << "interrupting";
thread.requestInterruption();
});
});
return a.exec();
}

打印

starting
loop 0
loop 1
interrupting
loop 2
finished

。不出所料,在"中断"one_answers"中断"之间有几秒钟的延迟。和循环2,循环2之间没有延迟和"finished".

如果你想更细粒度地中断你的任务,那么你必须找到一个比ZipFile::AddFile更细粒度的API,这样你就可以定期检查isInterruptionRequested(),即使一个文件被存档,或者使用一个可以被杀死的外部进程。

最新更新