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. Stopping the execution of a QThread when a signal is emitted from another thread

Stopping the execution of a QThread when a signal is emitted from another thread

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.5k 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.
  • S Offline
    S Offline
    sergex
    wrote on 22 Nov 2014, 18:16 last edited by
    #1

    Hello,

    In my application I have two intensive operations that I start a new Thread to run them. What I am trying to accomplish is that when for example Thread 1 is finished, I want to end Thread 2. Only if Thread 1 is done I will want to end the execution of the function in Thread 2.

    How I am starting my Threads is:
    @

       //Thread 1
        QThread* thread = new QThread;
        Worker* worker = new Worker; // AQObject 
        worker->moveToThread(thread);
    
        connect(thread, SIGNAL(started()), worker, SLOT(process()));   //in process the intensive function is called 
        connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
        connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();
    
      Q_EMIT startThread2(); // Signal connected to slot that starts thread 2 in the same way. 
    

    @

    How Could I go about doing this?

    Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Nov 2014, 22:14 last edited by
      #2

      Hi,

      What about:

      @connect(worker, SIGNAL(finished()), thread2, SLOT(start()));@

      ?

      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
      • S Offline
        S Offline
        sergex
        wrote on 25 Nov 2014, 17:58 last edited by
        #3

        I think I managed to stop the execution of the thread when I'm supposed to. However I am having the need to call a slot which is in the Qobject (Worker above) by emitting a signal from the main thread.

        I tried doing a connection:

        From main thread I just added this connection below :

        @
        .....
        connect(this, SIGNAL(callSlot()), worker, SLOT(mySlotToExecute()));

        thread->start();

        .....
        @

        The signal callSlot is emitted after a key press.

        But the slot is never called. Is this even possible somehow?

        Thanks.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Nov 2014, 23:21 last edited by
          #4

          How do you know the slot is not called ?

          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
          • S Offline
            S Offline
            sergex
            wrote on 26 Nov 2014, 15:13 last edited by
            #5

            Because I have a qdebug() there and it never gets printed in the console and the process started doesn't stop like it should if that slot would be called.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 26 Nov 2014, 22:32 last edited by
              #6

              Did you checked on the console whether you got any error message concerning the connection ?

              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

              5/6

              26 Nov 2014, 15:13

              • Login

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