Sorry I’m not experienced enough to understand the error I’m getting and I have been trying now for weeks to understand what the problem is.
WHAT I NEED TO ACCOMPLISH:
Run multiple threads to control hardware and tests at the same time.
MY CODE:
I Followed the example provided here.
This example is also available on many other sites so clearly it must work.
In my main
code I have:
MotorClass *MotorObj = new MotorClass;
QThread *MotorThread = new QThread;
MotorObj->moveToThread(MotorThread);
connect(MotorThread, SIGNAL(started()), MotorObj, SLOT(RunMotor()));
connect(MotorObj, SIGNAL(finished()), MotorThread, SLOT(quit()));
connect(MotorObj, SIGNAL(finished()), MotorObj, SLOT(deleteLater()));
connect(MotorThread, SIGNAL(finished()), MotorThread, SLOT(deleteLater()));
MotorThread->start();
In my MotorClass.h
I have:
class MotorClass : public QObject
{
Q_OBJECT
public:
explicit MotorClass(QObject *parent = 0);
~MotorClass();
public slots:
void RunMotor();
signals:
void finished();
};
In my MotorClass.cpp
I have:
MotorClass::MotorClass(QObject *parent) : QObject(parent)
{
}
MotorClass::~MotorClass()
{
}
void MotorClass::RunMotor()
{
qDebug("running");
emit finished();
}
MY PROBLEM:
qDebug()
does display the “running” message, but then it is followed by the following output and the program crashes.
QWinEventNotifier: event notifiers cannot be disabled from another thread
QWinEventNotifier: event notifiers cannot be disabled from another thread
QWinEventNotifier: event notifiers cannot be disabled from another thread
The program has unexpectedly finished.
WHAT I HAVE TRIED SO FAR:
If I comment out the connect
code that is suppose to close the MotorThread
and MotorObj
, the errors disappear but obviously after running the program for long periods at a time causes it to crash. I understand this is because the MotorObj
and MotorThread
aren't being closed.
I have tried other methods like inheriting the QThread
class and then re-implementing the run()
function but this does not cover my requirements and is also not the recommended way of using QThread
s.
Any Ideas where I’m going wrong???
Looks like you are deleting the MotorThread after first execution has finished. So on next
MotorThread->start()
the program should crash, since it's only a dangling pointer. However, even if you remove the last connect, also the MotorObj does not exist anymore, so the slot might only be called once.Why dont you call the
deleteLater()
right after the objects aren't needed anymore!?