thread Synchronous problem
i start a thread to handle somethings ,gui wait for the things process .when end , notify the gui thread go run .here is my code :
Code:
{
Q_OBJECT
public:
Worker (){m_hEvent = NULL ;};
public slots:
void doWork
(const QString ¶meter
) { // ...
setEvent(m_hEvent);
}
public :
void SetSyncEvent(Handle hEvent){m_hEvent = hEvent};
signals:
void resultReady
(const QString &result
);
private:
Handle m_hEvent ;
};
{
Q_OBJECT
public:
Controller() {
m_hEvent = ::CreateEvent(NULL,FALSE,FALSE,NULL);
Worker *worker = new Worker;
worker->SetEvent(m_hEvent);
worker->moveToThread(&workerThread);
connect(workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
// connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
~Controller() {
workerThread.quit();
workerThread.wait();
}
void Oper()
{
WaitForSingleObject(m_hEvent,INFINITE);
.......
}
public slots:
// void handleResults(const QString &);
signals:
private:
Handle m_hEvent ;
};
void main()
{ Controller controle ; controle.oper();
}
but sometimes i found after worker class’s function doWork run the last code setevent(m_hevent), the app can not get the event and the WaitForSingleObject(m_hEvent,INFINITE) always in wait state . i use vs2008 qt 5.2.1 ,i found the workerThread can not exit the thread ,can anyone tell me why and how can i solved this problem .
Re: thread Synchronous problem
First question is why you even want to use a thread if you block the main thread anyway?
Usually a worker thread is used to do something long going and potentially block while keeping the UI responsive.
Cheers,
_