Index: sources/canbus/messagedispatcher.cpp =================================================================== diff -u -re1605219ac2baf49ef21d0889f845ac53d59c3c1 -r56d00a82669a7a2c00ab90109a89dbec8db27527 --- sources/canbus/messagedispatcher.cpp (.../messagedispatcher.cpp) (revision e1605219ac2baf49ef21d0889f845ac53d59c3c1) +++ sources/canbus/messagedispatcher.cpp (.../messagedispatcher.cpp) (revision 56d00a82669a7a2c00ab90109a89dbec8db27527) @@ -14,6 +14,7 @@ #include "messagedispatcher.h" // Qt +#include #include // Project @@ -34,33 +35,40 @@ */ bool MessageDispatcher::init() { - // This is required for Signal/Slots in threading. - qRegisterMetaType("Can_Id"); + if ( _init ) return false; + _init = true; - // runs in main thread - Q_ASSERT_X(QThread::currentThread() == qApp->thread() , "_MessageDispatcher::init", "The Class initialization must be done in Main Thread" ); - _CanMessage_Thread.setObjectName("Can Message Thread"); - connect(qApp, &QApplication::aboutToQuit, this, &MessageDispatcher::quit); - _CanMessage_Thread.start(); - moveToThread(&_CanMessage_Thread); - - // runs in USBWatcher thread initConnections(); - LOG_EVENT(QObject::tr("MessageDispatcher Initialized")); + LOG_EVENT(QObject::tr("%1 Initialized").arg(metaObject()->className())); return true; } -void MessageDispatcher::quit() +/*! + * \brief MessageDispatcher::init + * \details Initialized the Class by calling the init() method first + * And initializes the thread vThread by calling initThread + * on success init(). + * \param vThread - the thread + * \return returns the return value of the init() method + */ +bool MessageDispatcher::init(QThread &vThread) { - // runs in Logger thread - moveToThread(qApp->thread()); + if ( init() ) return false; + initThread(vThread); + return true; +} - // runs in main thread - _CanMessage_Thread.quit(); - _CanMessage_Thread.wait(); +/*! + * \brief MessageDispatcher::quit + * \details quits the class + * Calls quitThread + */ +void MessageDispatcher::quit() +{ + quitThread(); } /*! @@ -80,6 +88,37 @@ } /*! + * \brief ApplicationController::initThread + * \details Moves this object into the thread vThread. + * And checks that this method is called from main thread. + * Also connects quitThread to application aboutToQuit. + * \param vThread - the thread + */ +void MessageDispatcher::initThread(QThread &vThread) +{ + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); + _thread = &vThread; + _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); + connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); + _thread->start(); + moveToThread(_thread); +} + +/*! + * \brief MessageDispatcher::quitThread + * \details Moves this object to main thread to be handled by QApplicaiton + * And to be destroyed there. + */ +void MessageDispatcher::quitThread() +{ + if ( ! _thread ) return; + + // runs in thread + moveToThread(qApp->thread()); +} + +/*! * \brief MessageDispatcher::onFrameReceive * \details Upon message has been received over CANBUS this slot will be called * by FrameInterface::didFrameReceive signal to process the frame