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. Exit SLOT when destroying QThread

Exit SLOT when destroying QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.8k Views 1 Watching
  • 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hello,
    I have a class "Client" witch will create a QThread and an object of type Writer2

    class Client : public QObject
    {
        Q_OBJECT
        QThread workerThread;
      
    Client(){
    Thread workerThread;
     Writer2 *writer2 = new Writer2();
    writer2->moveToThread(&workerThread);
    }
    }
    

    //Writer2.h has a SLOT 'testSlot()'

    class Writer2 : public QObject
    {
        Q_OBJECT
    
    public slots :
    
     void testSlot(){
            bool result = readAck() // a test
           if(result ){
            emit ack_emerStop(true);
                 qDebug()<<"ack OK...";
               return;           //   if readAck() returns true  its ok
           }   
           else{             // if readAck() returns false, I wait 500ms and recall this slot (LOOP)
               QThread::msleep(500);
               qDebug()<<"reading ack again ...";
                testSlot();      // recall 
           }
        }
    }
    

    My probleme is, if my testSlot() is looping, i can't quit my app properly... because QThread will be deleted but SLOT is still running in a thread.
    So how to interrupt this SLOT execution in case i quit the application ?

    in client.cpp i have tryed lot of things unsuccessfully..

    // QObject::connect(QCoreApplication::instance(),SIGNAL(aboutToQuit()),&workerThread,SLOT(deleteLater()));
    // QObject::connect(this,SIGNAL(destroyed(QObject*)),&workerThread,SLOT(quit())); // terminate
    //QObject::connect(&workerThread,SIGNAL(finished()),writer2,SLOT(deleteLater()))

    Thank you in advance,
    LA

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

      Hi,

      Do you mean you want to use an infinite loop in testSlot ?

      By the way, you have something wrong in your Client constructor. You are creating a local workerThread object which is going to be destroyed by the end of the constructor which is likely not what you want.

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

      ODБOïO 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Do you mean you want to use an infinite loop in testSlot ?

        By the way, you have something wrong in your Client constructor. You are creating a local workerThread object which is going to be destroyed by the end of the constructor which is likely not what you want.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @SGaist Hi,
        thank you for answering

        Yes, if 'readAck()' return 'false', I want 'testSlot()' to loop until 'readAck()' returns 'true' or user quit application ..

        For the Client constructor part, its ok. I want to destroy everything when client is destructed.

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

          Well, no it's not really. You are moving your object to another thread that is destroyed at the end of the constructor which doesn't really make sense.

          As for your loop, why not use a QTimer to do the check periodically rather than that recursive loop ?

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

          ODБOïO 1 Reply Last reply
          1
          • SGaistS SGaist

            Well, no it's not really. You are moving your object to another thread that is destroyed at the end of the constructor which doesn't really make sense.

            As for your loop, why not use a QTimer to do the check periodically rather than that recursive loop ?

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #5

            @SGaist thx.
            I thought about using QTimer, i will try it.

            @SGaist said in Exit SLOT when destroying QThread:

            Well, no it's not really. You are moving your object to another thread that is destroyed at the end of the constructor which doesn't really make sense.

            I'm using "Writer" to communicate with a PLC (write/read data).
            It has Pubic slots

            Client has Signals connected to that slots

            As the read or write operations can take some time,
            I move a writer object to new thread to dont block the main thread.Maybe this is not the best way to do this but It is doing the job as expected.

            Please tell me how would you advise me to do it if you have an idea?
            thx

            jsulmJ 1 Reply Last reply
            0
            • ODБOïO ODБOï

              @SGaist thx.
              I thought about using QTimer, i will try it.

              @SGaist said in Exit SLOT when destroying QThread:

              Well, no it's not really. You are moving your object to another thread that is destroyed at the end of the constructor which doesn't really make sense.

              I'm using "Writer" to communicate with a PLC (write/read data).
              It has Pubic slots

              Client has Signals connected to that slots

              As the read or write operations can take some time,
              I move a writer object to new thread to dont block the main thread.Maybe this is not the best way to do this but It is doing the job as expected.

              Please tell me how would you advise me to do it if you have an idea?
              thx

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @LeLev

              Client(){
                  Thread workerThread;
                  Writer2 *writer2 = new Writer2();
                  writer2->moveToThread(&workerThread);
              }
              

              workerThread will be destroyed as soon as the constructor finishes. That means: after construction of Client there is no thread anymore! workerThread should be member variable in Client, not a local variable in its constructor.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              ODБOïO 1 Reply Last reply
              1
              • jsulmJ jsulm

                @LeLev

                Client(){
                    Thread workerThread;
                    Writer2 *writer2 = new Writer2();
                    writer2->moveToThread(&workerThread);
                }
                

                workerThread will be destroyed as soon as the constructor finishes. That means: after construction of Client there is no thread anymore! workerThread should be member variable in Client, not a local variable in its constructor.

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by
                #7

                @jsulm said in Exit SLOT when destroying QThread:

                Thread workerThread;

                Hi,
                thx !
                The code i pasted is not my real code.. i have tryed to summarize but I made errors... In fact QThread workerThread is a member.. this is not my probleme.

                @SGaist said in Exit SLOT when destroying QThread:

                As for your loop, why not use a QTimer to do the check periodically rather than that recursive loop ?
                Use of QTimer solved my probleme !

                THX @SGaist && @jsulm
                LA

                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