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



  • 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.


  • Lifetime Qt Champion

    Hi,

    What about:

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

    ?



  • 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.


  • Lifetime Qt Champion

    How do you know the slot is not called ?



  • 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.


  • Lifetime Qt Champion

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


Log in to reply
 

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