Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QThread with a DLL

How to use QThread with a DLL

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ZoptuneZ Offline
    ZoptuneZ Offline
    Zoptune
    wrote on last edited by Zoptune
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ZoptuneZ 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        ZoptuneZ Offline
        ZoptuneZ Offline
        Zoptune
        wrote on last edited by Zoptune
        #3

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          ZoptuneZ 1 Reply Last reply
          0
          • SGaistS SGaist

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

            ZoptuneZ Offline
            ZoptuneZ Offline
            Zoptune
            wrote on last edited by
            #5

            @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();
            };
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              ZoptuneZ 1 Reply Last reply
              0
              • SGaistS SGaist

                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

                ZoptuneZ Offline
                ZoptuneZ Offline
                Zoptune
                wrote on last edited by
                #7

                @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 ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  That's the base idea yes.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  ZoptuneZ 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    That's the base idea yes.

                    ZoptuneZ Offline
                    ZoptuneZ Offline
                    Zoptune
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved