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:- creating a global thread and global QCoreApplication, moving the application to thread and executing
- 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):
- if I try to quit the serial port thread, the thread doesn't quit.
- sometimes it crashes.
My question is: what is the correct way to do it?
thanks
-
Hi,
I'm not sure if you can safely move a QCoreApplication to another thread. However, I know this works:
- Create a new thread using std::thread (NOT a QThread)
- In that thread, run a function that constructs the QCoreApplication, and then constructs the QSerialPort, and then call QCoreApplication::exec().
- Make sure that you only call QSerialPort methods in that thread.
-
Hi,
I'm not sure if you can safely move a QCoreApplication to another thread. However, I know this works:
- Create a new thread using std::thread (NOT a QThread)
- In that thread, run a function that constructs the QCoreApplication, and then constructs the QSerialPort, and then call QCoreApplication::exec().
- 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!
- make a global instance of QCoreApplication*
- create a wrapper class containing a QThread object and a QSerialPort object
- the DLL shares with app 2 functions, one for creation and the other for Release
- 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.
- 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!