c++ - Cannot kill Qt thread with CryptoPP FileSink running in the thread run method -
i have ui app in c++ , qt5.4, im using cryptopp 5.6.2 encrypt files. ran following problem:
when encrypt button hit, new thread 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 thread , worker in mainwindow class (parent of encrypt button , 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 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 thread run , encrypting file file b on fly, using function:
cryptopp::filesource(sourcefilename, true, new cryptopp::streamtransformationfilter( encryptor, new cryptopp::filesink(destfilename), cryptopp::blockpaddingschemedef::pkcs_padding ), true // streamtransformationfilter ); // filesource
but, thread stuck until file finishes encoding , writing, can take few minutes. have no way kill thread, there no place put isalive()
check.
i trying find solution allow me use filesource, filesink (speed compared fstream or file or qfile amazing) , let me cancel operation @ point.
i solved progress monitoring adding thread checks size of new being created encrypted file b, cool have control on bytes being written @ given moment (so can check isalive()
, increment data counters).
at point stuck , cannot find solution. please help.
you try passing false
second parameter (pumpall
) of filesource
constructor , doing work in loop in chunks using pump
method - should allow check isalive
, increment counters.
Comments
Post a Comment