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


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    From what you wrote, you already have a class that handles the communication with the device, right ?



  • @SGaist

    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();
    };
    

  • Lifetime Qt Champion

    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



  • @SGaist

    If i have well understood :

    I have a MachineController object that have a Machine object.
    Then i move the controller to the thread and i communicate between main app and controller using signals and slots ?


  • Lifetime Qt Champion

    That's the base idea yes.



  • Thank you @SGaist , Now i understand how to use QThread :)


  • Lifetime Qt Champion

    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 :)


Log in to reply
 

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