How to use QThread with a DLL
-
Hi,
I use a DLL provided by a manufacturer to give orders to a machine. This machine is used very often and and the time of execution of instructions is long so i decided to use QThread.
I have a view for a data feedback and i have created a class "machine" where i load the DLL and all the functions i need, i also create tables for parameters of the machine, a result table, and a string error.
But i don't know how to use my machine object in the QThread.
I have read this article about proper use of QThread : Qthread proper use
But i have questions :
- Because the machine is used very often do i need to create a thread that will be stop when the main app is closed or do i need to create and stop a thread every time i send orders to the machine ?
- How can i use different functions (for different orders) with different parameters in the thread ?
- How can i access to result table an error or modify parameters ?
Thx
-
Hi,
Looks rather like you you should create a worker object that does the communication with your machine. Then move that worker object in another thread.
-
Hi,
Looks rather like you you should create a worker object that does the communication with your machine. Then move that worker object in another thread.
@SGaist
Hi and thx for your answer,I'm new to QThread and i don't understand your idea. Can you please explain it more in details.
What are relations between worker and machine ? Where do i instantiate them ? Where do i load the DLL ? ...I thought that my machine class would be my worker because all my time consuming functions are defined in the machine class.
Thank you
-
From what you wrote, you already have a class that handles the communication with the device, right ?
-
From what you wrote, you already have a class that handles the communication with the device, right ?
Yes i have a device class :
// device definition // Functions from the DLL typedef int (WINAPI *fct1_ptr) (); typedef int (WINAPI *fct2_ptr) (); typedef int (WINAPI *fct3_ptr) (); class Device { fct1_ptr fct1; fct2_ptr fct2; fct3_ptr fct3; QLibrary lib; QString deviceError; QHash<QString,QString> deviceParam1; // Different parameters for different actions QHash<QString,QString> deviceParam2; QHash<QString,QString> deviceParam3; QHash<QString,QString> deviceParam4; QMultiMap<QString, QString> results; public: Device(); ~Device(); void init(); void action1(...); // use DLL fct1, fct2 or fct3 void action2(...); // use DLL fct1, fct2 or fct3 void action3(...); // use DLL fct1, fct2 or fct3 void setDeviceParam(); QMap<QString,QString> getResults(); QString getDeviceError(); };
-
So this class does the low-level communication with the machine.
Then you should write a QObject based class (e.g. MachineController) that you will interact with in your application using signals and slots and that will have an object of the Device class to do the actual communication with machine.
That will be your worker object that you will move to the QThread object like shown in QThread's documentation
-
So this class does the low-level communication with the machine.
Then you should write a QObject based class (e.g. MachineController) that you will interact with in your application using signals and slots and that will have an object of the Device class to do the actual communication with machine.
That will be your worker object that you will move to the QThread object like shown in QThread's documentation
-
That's the base idea yes.
-
You're welcome !
If that answers your question then please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)