event loop in Qt dll loaded by MFC App



  • Hello everybody,

    I have to develop a shared dll that uses serial communication (QSerialPort) with very reatricted time constraints.
    I would like to use readyRead signal in order to process data everytime they arrive.
    the DLL is loaded by an MFC application, so I have no instance of QCoreApplication and I have to create it in the dll.
    How can achieve this?
    I'm trying this way:

    1. creating a global thread and global QCoreApplication, moving the application to thread and executing
    2. creating another thread to move QSerialPort object and connecting readyRead with a slot.

    Doing this the dll works, but i have a lot of problems during the dll unload process (called by MFC FreeLibrary):

    1. if I try to quit the serial port thread, the thread doesn't quit.
    2. sometimes it crashes.

    My question is: what is the correct way to do it?

    thanks


  • Moderators

    Hi,

    I'm not sure if you can safely move a QCoreApplication to another thread. However, I know this works:

    1. Create a new thread using std::thread (NOT a QThread)
    2. In that thread, run a function that constructs the QCoreApplication, and then constructs the QSerialPort, and then call QCoreApplication::exec().
    3. Make sure that you only call QSerialPort methods in that thread.


  • @JKSH thank you for the response!!!

    Before reading your solution i founded a similar way to do!

    1. make a global instance of QCoreApplication*
    2. create a wrapper class containing a QThread object and a QSerialPort object
    3. the DLL shares with app 2 functions, one for creation and the other for Release
    4. The create function create the QCoreApplication instance if there isn't already ( check if the app is Qt or not) and create the instance of the wrapper class. The wrapper class create the Qthread, the QSerialPort object and move it to the thread. the wrapper has signal connected to the QSerialPort object slots.
    5. the release function destroy everything.

    the thing i din't though, is that i don't need to call QCoreApplication exec() to have an event loop. what is called here is the QThread::exec() function. I suppose that QCoreApplication in the constructor create just everything needed by the QThread to run and dispatch events!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.