wearilybird
10th May 2014, 04:58
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 :
class Worker : public QObject
{
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 ;
};
class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
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()
{
emit operate(QString("fsfs"));
WaitForSingleObject(m_hEvent,INFINITE);
.......
}
public slots:
// void handleResults(const QString &);
signals:
void operate(const QString &);
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 .
class Worker : public QObject
{
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 ;
};
class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
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()
{
emit operate(QString("fsfs"));
WaitForSingleObject(m_hEvent,INFINITE);
.......
}
public slots:
// void handleResults(const QString &);
signals:
void operate(const QString &);
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 .