Problèmes lors de la destruction
Bonjour à tous,
Je ne suis pas vraiment habituer à utiliser Qt, mais pour mon travail, je suis obliger de faire avec :D. J'essaie pour une application de faire un Worker générique pour faire mes actions en multithread. Voici mon code
ThreadWorker.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#ifndef _THREADWORKER_H_
#define _THREADWORKER_H_
#include "QtCore/qobject.h"
#include "QtCore/qthread.h"
#include "QtCore/qtimer.h"
class ThreadWorker : public QObject
{
Q_OBJECT
private:
int _frequency;
QTimer _timer;
public:
ThreadWorker(int frequency = 300);
~ThreadWorker();
/*!
\brief Create a worker which call the specified slot in th specified thread
\param thread The thread to use
\param source The object which contains the slot to use
\param slot The slot to use
*/
void createWorker(QThread *thread, const QObject* source, const char *slot);
/*!
\brief Sets the call frequency for the threaded method
\param frequency Frequency in millsecond (default value: 300ms)
*/
void setFrequency(int frequency);
private slots:
void start();
void stop();
public slots:
/*!
\brief a slot to indicates the end of our current work
*/
void terminate();
signals:
void finished();
};
#endif |
ThreadWorker.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include "ThreadWorker.h"
ThreadWorker::ThreadWorker(int frequency): _frequency(frequency), _timer(this)
{}
ThreadWorker::~ThreadWorker()
{
terminate();
}
void ThreadWorker::createWorker(QThread *thread, const QObject* source, const char *slot)
{
connect(&_timer, SIGNAL(timeout()), source, slot, Qt::DirectConnection);
connect(thread, SIGNAL(started()), this, SLOT(start()), Qt::DirectConnection);
connect(this, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
connect(this, SIGNAL(finished()), this, SLOT(stop()), Qt::DirectConnection);
moveToThread(thread);
}
void ThreadWorker::setFrequency(int frequency)
{
_frequency = frequency;
}
void ThreadWorker::start()
{
_timer.setInterval(_frequency);
_timer.start();
}
void ThreadWorker::stop()
{
if(_timer.isActive())
{
_timer.stop();
}
}
void ThreadWorker::terminate()
{
if(_timer.isActive())
{
emit finished();
}
} |
Et le code qui utilise le worker:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include "ThreadWorker.h"
class MyObject : public QObject
{
Q_OBJECT
private
QThread _thread;
ThreadWorker _worker;
public:
MyObject();
~MyObject();
void start();
void stop();
public slots:
void process();
};
MyObject::MyObject(): _thread(this)
{}
MyObject::~MyObject()
{
if(_thread.isRunning())
{
_worker.terminate();
_thread.wait(500);
}
}
void MyObject::start()
{
if(_thread.isRunning())
{
return;
}
_worker.createWorker(&_thread, this, SLOT(process()));
_thread.start();
}
void MyObject::process()
{
//do something
}
void MyObject::stop()
{
_worker.terminate();
} |
Et enfin, j'utilise le tout dans un test unitaire avec QTest avec la macro QTEST_MAIN.
Le déroulement se passe bien, jusqu'à la destruction du ThreadWorker où j'obtiens ceci
Citation:
Program: …
Module: 4.7.4
File: global\qglobal.cpp
Line:
ASSERT failure in QCoreApplication::sendEvent: “Cannot send events to objects owned by a different thread. Current thread 3f6a90.
Receiver ‘’ (of type ‘ThreadWorker’) was created in thread 12ef5c”, file kernel\qcoreapplication.cpp, line
Je ne vois pas vraiment d'où cela vient :/. Seul truc, c'est que cela ne se produit pas si je ne mets pas cette ligne moveToThread(thread);, cependant du coup, mon process() n'est plus jamais appelé.
Est ce que quelqu'un aurait la moindre idée de ce que je fais de mal ?
Merci d'avance de toute l'aide que vous pourrez m'apporter