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] Permanent Thread

[SOLVED] Permanent Thread

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.8k 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    Hi,

    I've read the "Threading Basics":http://qt-project.org/doc/qt-5.0/qtcore/thread-basics.html

    Before I did successfully do a thread by subclassing QThread and using run(), but I read this is not good use. My Thread needs to be permanent (lifetime of my application). I want to retrieve data from it at various time.

    So now I want to implement this kind of Threa


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      That page is outdated. Those examples will be removed in the Qt 5.2 documentation.

      You don't need to implement your own thread for your purpose. Just create a new QThread(). See the first example in the "QThread documentation":http://doc-snapshot.qt-project.org/qt5-stable/qthread.html#details

      Also, see the "Multithreading Technologies in Qt":http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html page for different ways to use threads in Qt -- subclassing QThread is ok for certain uses (but not for permanent threads like yours)

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        Thank you JKSH,

        The example supplied worked perfectly for my needs, now I need to figure out how to exchange data from the controller and the worker, I think signal and slot will do the job just fine :)

        @////////////////////////////////////////////////////////////////////////////////
        /// CONSTRUCTOR
        ////////////////////////////////////////////////////////////////////////////////
        DialogAnt::DialogAnt(QWidget *parent) :QDialog(parent),ui(new Ui::DialogAnt) {
        ui->setupUi(this);

        myHub = new Hub();  //cannot move object with a parent, so no parent and delete in destructor
        myHub->moveToThread(&workerThread);
        
        connect(myHub, SIGNAL(onStickFound()), this, SLOT(stickFound()) );
        connect(myHub, SIGNAL(onStickFound()), myHub, SLOT(Start()) );
        
        workerThread.start();
        myHub->InitUSBStick();
        

        }@


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          [quote author="maximus" date="1384266474"]now I need to figure out how to exchange data from the controller and the worker, I think signal and slot will do the job just fine :)[/quote]Yes :) Use signals and slots to communicate both ways.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maximus
            wrote on last edited by
            #5

            Hi again,

            My thread is working fine but for terminating the thread, i'm facing some difficulties.

            When I try to close my main GUI, the app becomes "not responding" trying to shut down the thread.
            The only way I was able to shut it down is with workerThread.terminate() but then I get error at shutdown

            My Thread is also running a thread inside it.
            I tell it to stop with the flag "threadRunning" but still I get stop on the .wait in the main GUI.
            Any idea on how to force the thread to stop without having a crash?

            Thanks in advance!

            [EDIT: The problem is in the children thread, it is not stopped before I stop it's parent thread, Stopping the children thread then the parent thread worked]

            @#include "dialogant.h"
            #include "ui_dialogant.h"
            #include <QDebug>
            #include "util.h"

            ////////////////////////////////////////////////////////////////////////////////
            /// CONSTRUCTOR
            ////////////////////////////////////////////////////////////////////////////////
            DialogAnt::DialogAnt(QWidget *parent) :QDialog(parent),ui(new Ui::DialogAnt) {
            ui->setupUi(this);

            myHub = new Hub();  //cannot move object with a parent, so no parent and delete in destructor
            myHub->moveToThread(&workerThread);
            
            connect(myHub, SIGNAL(onStickFound()), this, SLOT(stickFound()) );
            connect(this, SIGNAL(pairHR()), myHub, SLOT(Start()) );
            connect(myHub, SIGNAL(hrChanged(int)), this, SLOT(show_hr(int)) );
            
            workerThread.start();
            myHub->InitUSBStick();
            

            }

            ////////////////////////////////////////////////////////////////////////////////
            /// DESTRUCTOR
            ////////////////////////////////////////////////////////////////////////////////
            DialogAnt::~DialogAnt() {
            workerThread.quit();
            workerThread.wait();
            delete myHub;
            delete ui;
            }

            ////////////////////////////////////////////////////////////////////////////////
            /// CloseEvent
            ////////////////////////////////////////////////////////////////////////////////
            void DialogAnt::closeEvent(QCloseEvent *event) {

            qDebug() << "CLOSING WINDOW";
            

            // disconnect(myHub, SIGNAL(onStickFound()), this, SLOT(stickFound()) );
            // disconnect(this, SIGNAL(pairHR()), myHub, SLOT(Start()) );
            // disconnect(myHub, SIGNAL(hrChanged(int)), this, SLOT(show_hr(int)) );

            // myHub->Close();

            // workerThread.terminate(); works but shows error when app close
            myHub->threadRunning = false;
            myHub->Close();
            workerThread.quit();
            workerThread.wait();
            }@


            Free Indoor Cycling Software - https://maximumtrainer.com

            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