我在C++和QT5.4中有一個UI應用程序,我使用CryptoPP 5.6.2來加密文件。我遇到了以下問題:在線程運行方法中運行CryptoPP FileSink無法殺死Qt線程
當加密按鈕被擊中,一個新的線程啓動基於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();
我存儲指針(該加密鍵的親本和因此時隙)的螺紋和工人在主窗口類
工人的階層:
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; }
現在,當線程運行時,我正在使用此功能將文件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()
並增加數據計數器)。
在這一點上,我卡住了,找不到解決方案。請幫忙。
好問題。 @Yachoor爲你解決了問題。 – jww