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. Waiting for QThreads to finish
Forum Updated to NodeBB v4.3 + New Features

Waiting for QThreads to finish

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 7.4k Views 4 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.
  • A Offline
    A Offline
    ajaxcrypto
    wrote on last edited by
    #1

    I have a data model for a Qt GUI I am writing. There can be multiple async update to the model and the async task completion time may not mimic the order of starting time. I want to update GUI, only when all running async tasks have finished. (The async tasks are QThreads).

    How can I implement this in Qt?

    Taz742T 1 Reply Last reply
    0
    • A ajaxcrypto

      I have a data model for a Qt GUI I am writing. There can be multiple async update to the model and the async task completion time may not mimic the order of starting time. I want to update GUI, only when all running async tasks have finished. (The async tasks are QThreads).

      How can I implement this in Qt?

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2

      @ajaxcrypto
      Excuse me, you say that while QThread working, do not click on Form (QPushbutton, QLienedit ...)?
      If not, why do you need QThread?

      Do what you want.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ajaxcrypto
        wrote on last edited by
        #3

        No, I want the application to be responsive, the async operation does not block the GUI, and there might be multiple async operations doing different tasks. But, once they have all finished, I want to update something in the GUI i.e. execute a function from GUI thread i.e.

        start_multiple_threads();
        QObject::connect(??, magic_all_other_threads_finished_signal, guiObject, []{ doSomething(); });
        
        kshegunovK Taz742T 2 Replies Last reply
        0
        • A ajaxcrypto

          No, I want the application to be responsive, the async operation does not block the GUI, and there might be multiple async operations doing different tasks. But, once they have all finished, I want to update something in the GUI i.e. execute a function from GUI thread i.e.

          start_multiple_threads();
          QObject::connect(??, magic_all_other_threads_finished_signal, guiObject, []{ doSomething(); });
          
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          Please post some code of how you set up the threads and how you signal the finish of the operation. What threading technology do you use?

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2
          • A ajaxcrypto

            No, I want the application to be responsive, the async operation does not block the GUI, and there might be multiple async operations doing different tasks. But, once they have all finished, I want to update something in the GUI i.e. execute a function from GUI thread i.e.

            start_multiple_threads();
            QObject::connect(??, magic_all_other_threads_finished_signal, guiObject, []{ doSomething(); });
            
            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by Taz742
            #5

            @ajaxcrypto said in Waiting for QThreads to finish:

            No, I want the application to be responsive, the async operation does not block the GUI, and there might be multiple async operations doing different tasks. But, once they have all finished, I want to update something in the GUI i.e. execute a function from GUI thread i.e.

            You dont need a finished SIGNAL. You need to understand your gui that something happened.

            @ajaxcrypto

            signals:
                void loaded(int); 
            
            public slots:
                void run() {
                    int i = 0;
                    forever {
                        emit loaded(i++);
                        this->msleep(25);
                    }
                }
            

            @ajaxcrypto said in Waiting for QThreads to finish:

            start_multiple_threads();
            QObject::connect(??, magic_all_other_threads_finished_signal, guiObject, []{ doSomething(); });

            MyThread *thrd = new MyThread();
            connect(thrd,  &MyThread::loaded,  this,  [=](int value) {// do something});
            thrd->start();
            

            Do what you want.

            1 Reply Last reply
            1
            • BuckwheatB Offline
              BuckwheatB Offline
              Buckwheat
              wrote on last edited by
              #6

              There is a very good tutorial for QThreads at https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

              When you override run like you are, you are blocking the thread's event loop. You could do the same by having your worker object run in a 25ms timer. Connect the worker object to the thread as suggested in the article. Have the run entry point of the object start the timer and just emit your loaded signal as designed.

              This is truly the best way to run threads. You can then request termination and they will stop. For example:

              void Worker::start ()
              {
              m_timerId = startTimer (25);
              m_count = 0;
              }

              void Worker::timerEvent (QTimerEvent* event)
              {
              if (event->timerId () == m_timerId) {
              Q_EMIT loaded (m_count++);
              return;
              }
              QObject::timerEvent (event);
              }

              Worker* worker = new Worker; // QObject derived
              QThread* thread = new QThread;

              // Connect the signals for handling thread processing
              connect (thread, &QThread::started, worker, &start);
              connect (thread, &QThread::finished, worker, &finished);
              connect (this, &finished, worker, &deleteLater);
              connect (thread, &QThread::finished, thread, &QThread::deleteLater);

              // Move this object to the thread and start it
              worker->moveToThread (thread);
              thread->start ();

              blah blah blah

              thread->requestInteruption ();
              thread->wait ();

              This will clean up memory and not use the dreaded sleep but always work on the timer in the thread's own event queue.

              Dave Fileccia

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

                Hi,

                What kind of operations are you doing ?

                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
                • A Offline
                  A Offline
                  ajaxcrypto
                  wrote on last edited by
                  #8

                  Guys, I have solved the issue. I am using multiple concurrent running QThread, and using signals to update and know when all have finished.

                  1 Reply Last reply
                  1
                  • Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #9

                    @ajaxcrypto glad you solved your issue. Could it be possible to mark your post as such then? Thanks.

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    2

                    • Login

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