无法杀死Qt线程与CryptoPP FileSink运行在线程运行方法 [英] Cannot kill Qt thread with CryptoPP FileSink running in the thread run method
问题描述
我有一个UI应用程序在C + +和QT5.4,我使用CryptoPP 5.6.2加密文件。我遇到了以下问题:
-
当加密按钮被击中时,基于本教程。
//新线程
CryptoWorkerThread = new QThread;
this-> worker = new CryptoWorker(fileName.c_str(),newFileName.c_str(),key,keyLength,iv);
this-> worker-> moveToThread(CryptoWorkerThread);
connect(worker,SIGNAL(error(QString)),this,SLOT(errorString(QString)));
connect(CryptoWorkerThread,SIGNAL(started()),worker,SLOT(process()));
connect(worker,SIGNAL(finished()),CryptoWorkerThread,SLOT(quit()));
connect(worker,SIGNAL(finished()),this,SLOT(on_CryptoWorker_finished()));
connect(worker,SIGNAL(finished()),worker,SLOT(deleteLater());
connect(CryptoWorkerThread,SIGNAL(finished()),CryptoWorkerThread,SLOT(deleteLater()));
CryptoWorkerThread-> start();
- 我存储指向mainwindow类中的线程和worker的指针因此插槽)
Worker类:
class CryptoWorker: public QObject {
Q_OBJECT
public:
CryptoWorker(const char * sourceFileName,const char * destFileName,const byte * key,int keyLength,const byte * iv);
〜CryptoWorker();
const char * sourceFileName;
const char * destFileName;
public slots:
void process();
信号:
void finished();
void error(QString err);
private:
//这里添加你的变量
const byte * key;
const byte * iv;
int keyLength;
};
CryptoWorker :: CryptoWorker(const char * sourceFileName,const char * destFileName,const byte * key,int keyLength,const byte * iv){
this-> sourceFileName = sourceFileName;
this-> destFileName = destFileName;
this-> key = key;
this-> keyLength = keyLength;
this-> iv = iv;
}
CryptoWorker ::〜CryptoWorker(){
}
void CryptoWorker :: process(){
CryptoPP :: CBC_Mode< CryptoPP :: AES> ::加密加密器(key,keyLength,iv);
CryptoPP :: FileSource(sourceFileName,true,
new CryptoPP :: StreamTransformationFilter(
encryptor,
new CryptoPP :: FileSink(destFileName)
CryptoPP :: BlockPaddingSchemeDef :: PKCS_PADDING
),
true // StreamTransformationFilter
); // FileSource
emit finished();
return;
}
线程运行,我正在使用此函数将文件A加密到文件B:
CryptoPP :: FileSource(sourceFileName ,true,
new CryptoPP :: StreamTransformationFilter(
encryptor,
new CryptoPP :: FileSink(destFileName),
CryptoPP :: BlockPaddingSchemeDef :: PKCS_PADDING
),
true // StreamTransformationFilter
); // FileSource
但是,线程卡住,直到文件完成编码和写入,分钟。我没有办法杀死线程,因为没有地方放置 isAlive()
检查。
我试图找到一个解决方案,将允许我使用FileSource,FileSink(速度比fstream或文件或qfile是惊人的),也让我取消操作在某一点。
通过添加另一个线程来解决进度监视,该线程检查新创建的加密文件B的大小,但是控制在给定时刻正在写入的字节将是很酷的(这样我可以检查
isAlive()
并增加数据计数器。) 此时,我遇到了无法找到解决方案。请帮助。
您可以尝试将 false
传递给第二个参数 FileSource
构造函数并使用 Pump 在块中循环执行工作
pumpAll
code>方法 - 这应该允许检查 isAlive
并增加计数器。
I have a UI app in C++ and QT5.4, Im using CryptoPP 5.6.2 to encrypt files. I ran into the following problem:
When the Encrypt button is hit, a new thread is started based on this tutorial.
// new thread CryptoWorkerThread = new QThread; this->worker = new CryptoWorker(fileName.c_str(), newFileName.c_str(), key, keyLength, iv); this->worker->moveToThread(CryptoWorkerThread); connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString))); connect(CryptoWorkerThread, SIGNAL(started()), worker, SLOT(process())); connect(worker, SIGNAL(finished()), CryptoWorkerThread, SLOT(quit())); connect(worker, SIGNAL(finished()), this, SLOT(on_CryptoWorker_finished())); connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater())); connect(CryptoWorkerThread, SIGNAL(finished()), CryptoWorkerThread, SLOT(deleteLater())); CryptoWorkerThread->start();
I store pointer to the thread and worker in the mainwindow class (parent of the encrypt button and thus the slot)
Worker Class:
class CryptoWorker : public QObject { Q_OBJECT public: CryptoWorker(const char* sourceFileName, const char* destFileName, const byte * key, int keyLength, const byte * iv); ~CryptoWorker(); const char* sourceFileName; const char* destFileName; public slots: void process(); signals: void finished(); void error(QString err); private: // add your variables here const byte* key; const byte* iv; int keyLength; }; CryptoWorker::CryptoWorker(const char* sourceFileName, const char* destFileName, const byte * key, int keyLength, const byte * iv){ this->sourceFileName = sourceFileName; this->destFileName = destFileName; this->key = key; this->keyLength = keyLength; this->iv = iv; } CryptoWorker::~CryptoWorker(){ } void CryptoWorker::process(){ CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryptor(key, keyLength, iv); CryptoPP::FileSource(sourceFileName, true, new CryptoPP::StreamTransformationFilter( encryptor, new CryptoPP::FileSink(destFileName), CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING ), true // StreamTransformationFilter ); // FileSource emit finished(); return; }
Now, when the thread is run and I am encrypting file A to file B on the fly, using this function:
CryptoPP::FileSource(sourceFileName, true,
new CryptoPP::StreamTransformationFilter(
encryptor,
new CryptoPP::FileSink(destFileName),
CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING
),
true // StreamTransformationFilter
); // FileSource
But, the thread is stuck until the file finishes encoding and writing, which can take few minutes. I have no way to kill the thread, as there is no place to put an isAlive()
check.
I am trying to find a solution that will allow me to use FileSource, FileSink (speed compared to fstream or file or qfile is amazing) and also let me cancel the operation at some point.
I solved progress monitoring by adding another thread that checks size of the new being created encrypted file B, but it would be cool to have control over the bytes that are being written at a given moment (so that I can check isAlive()
and increment data counters).
At this point I am stuck and cannot find a solution. Please help.
You could try passing false
to second parameter (pumpAll
) of FileSource
constructor and doing the work in a loop in chunks using Pump
method - this should allow to check isAlive
and increment the counters.
这篇关于无法杀死Qt线程与CryptoPP FileSink运行在线程运行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!