스레드가 실행을 완료하면 개체에 알리고 싶습니다. 그러나 스레드를 올바르게 종료 할 수 없습니다. ThreadWorker.cppQThread - 슬롯을 사용하여 종료 스레드를 종료하려면
void ThreadWorker::doSetup(QThread *thread, const string &path)
{
_strPath = path;
connect(thread, SIGNAL(started()), this, SLOT(run()));
connect(this, SIGNAL(finished()), thread, SLOT(quit())); //tried terminate() also
}
void ThreadWorker::run()
{
DirectorySearch dicSearch;
vector<string> vecFileList = dicSearch.getFileList(_strPath);
emit updateStatus("Directory Fetched");
emit finished();
}
(가) 종료
thread = new QThread;
tw = new ThreadWorker;
connect(tw, SIGNAL(updateStatus(QString)), this, SLOT(statusUpdate(QString)));
tw->doSetup(thread, strDic);
tw->moveToThread(thread);
thread->start();
while(thread->isRunning())
{
}
qDebug() << "Thread Finished";
은() 슬롯이 스레드를 중지하지 않는 것 (QThread :: isFinished는 결코 진정한 반환
Processor.cpp : 나는 다음과 같은 코드가 있습니다). 누군가 올바른 방향으로 나를 안내 할 수 있습니까?
(참고 : ThreadWorker는 QThread에서 상속하지 않습니다) ...
테스트를 위해 ... while 루프에서 QCoreApplication :: instance() -> processEvents()를 추가하십시오. 이것은 제안 일 뿐이므로 답변으로 게시하지 않습니다. 이것은 QThread 자체가 주 스레드에 있기 때문에 작동 할 수 있습니다. 따라서 QThread에서 실행해야하는 ThreadWorker 슬롯 대신 OWN 슬롯이 기본 이벤트 루프에서 호출 될 수 있습니다. – Raiv
@Raiv : spot-on, 나는 정상적인 작동에서'terminate()'를 사용하도록 권유받은 –