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. [SOLVED] Close a Qthread in the middle of it
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Close a Qthread in the middle of it

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.2k 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.
  • X Offline
    X Offline
    XGuy
    wrote on last edited by
    #1

    i have a QDialog and inside it's constructor a start a thread. but what if the user closes the dialog before thread is finished ? my solution was to implement closeEvent of the dialog so that i can quit the thread but still my app crashes ;-(

    here is my code:

    @
    MyDialog::MyDialog() :
    ui(new Ui::MyDialog)
    {
    QTimer::singleShot(50, this, SLOT(startThread()));
    }

    void MyDialog::startThread()
    {
    QObject::connect(&thrTest, SIGNAL(started()), &objAnotherClass, SLOT(sampleSlot()));
    QObject::connect(&objAnotherClass, SIGNAL(sampleSlotFinished(int)), this, SLOT(sampleSlotFinished(int)));
    objAnotherClass.moveToThread(&thrTest);
    thrTest.start();
    }

    void MyDialog::sampleSlotFinished(int returnValue)
    {
    .
    .
    .
    // QUIT THREAD
    QObject::disconnect(&objAnotherClass, 0, 0, 0);
    QObject::disconnect(&thrTest, 0, 0, 0);
    thrTest.quit();
    thrTest.exit(0);
    }

    void MyDialog::closeEvent(QCloseEvent *event)
    {
    Q_UNUSED(event);
    if(thrTest.isRunning())
    {
    disconnect(&thrTest, 0, 0, 0);
    disconnect(&objAnotherClass, 0, 0, 0);
    thrTest.quit();
    thrTest.exit(0);
    thrTest.deleteLater();
    objAnotherClass.deleteLater();
    }
    }

    @

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Think in the closeEvent of the Dialog you need to wait until the QThread is done via:
      @
      while(isFinished() == false)
      @ check. Then do the deletion of them all.
      The problem is that your QThread object resides in the Dialog class. When you do a deleteLater on that one, if will get deleted when the dialog event queue is empty, but then the QThread object might not be ended. This probably causes the crash.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • X Offline
        X Offline
        XGuy
        wrote on last edited by
        #3

        thanks for reply.

        i changed close event to below code:

        @
        void MyDialog::closeEvent(QCloseEvent *event)
        {
        Q_UNUSED(event);
        if(thrTest.wait()) // block until thread is finished
        {
        disconnect(&thrTest, 0, 0, 0);
        disconnect(&objAnotherClass, 0, 0, 0);
        thrTest.deleteLater();
        objAnotherClass.deleteLater();
        }
        }
        @
        but the problem is if i wait on that thread! it will also block the main thread(Caller Thread) so application won't respond untill this thread is finished!

        1 Reply Last reply
        0
        • X Offline
          X Offline
          XGuy
          wrote on last edited by
          #4

          [quote author="Jeroentje@home" date="1383816067"]Think in the closeEvent of the Dialog you need to wait until the QThread is done via:
          @
          while(isFinished() == false)
          @ check. Then do the deletion of them all.
          The problem is that your QThread object resides in the Dialog class. When you do a deleteLater on that one, if will get deleted when the dialog event queue is empty, but then the QThread object might not be ended. This probably causes the crash. [/quote]

          thanks for reply.

          i changed close event to below code:
          @
          void MyDialog::closeEvent(QCloseEvent *event)
          {
          Q_UNUSED(event);
          if(thrTest.wait()) // block until thread is finished
          {
          disconnect(&thrTest, 0, 0, 0);
          disconnect(&objAnotherClass, 0, 0, 0);
          thrTest.deleteLater();
          objAnotherClass.deleteLater();
          }
          }
          @

          but the problem is if i wait on that thread! it will also block the main thread(Caller Thread) so application won’t respond untill this thread is finished!

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            Have a look at "this thread":http://qt-project.org/forums/viewthread/34537/ , the problem is similar to yours.

            1 Reply Last reply
            0
            • X Offline
              X Offline
              XGuy
              wrote on last edited by
              #6

              [quote author="KA51O" date="1383818721"]Have a look at "this thread":http://qt-project.org/forums/viewthread/34537/ , the problem is similar to yours.[/quote]

              thanks for the link.
              by reading that post i came to the idea of using boolean data members and always checking that members inside thread, sounds good!
              i will happily accept any other workarounds.

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                Chopping the worker object (that runs in the QThread) is a good idea and handling timing in the worker object. Then add a slot for the "StopObject" thread and your done. That's about the easiest way, so yes, checking boolean variables is not a problem. Or in the "stop" slot, close the file or other thing the thead has open. Delete all allocated memory and set this->deleteLater() and emit the finished signal to have the QThread that controls the object be closed and removed.
                That should work fine.

                Greetz, Jeroen

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  XGuy
                  wrote on last edited by
                  #8

                  Thank you all.
                  i also encourage people with the same problem have a look at these two links which perfectly and simply cover how to use Qthread
                  "1st":https://fabienpn.wordpress.com/2013/05/01/qt-thread-simple-and-stable-with-sources/

                  "2nd":https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                  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