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. Howto know whether the Qthread is running inside an object ?
Qt 6.11 is out! See what's new in the release blog

Howto know whether the Qthread is running inside an object ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 537 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.
  • P Offline
    P Offline
    pingal
    wrote on last edited by pingal
    #1

    I've implemented my Qthread as fol:

    Listening Listen2Clients;      // Inherits from QObject
    QThread *listening_thread = new QThread();
    Listen2Clients.moveToThread(listening_thread);
    
    QObject::connect(listening_thread, SIGNAL(started()), &Listen2Clients, SLOT(process()));
    QObject::connect(&Listen2Clients, SIGNAL(finished()), listening_thread, SLOT(quit()));
    
    listening_thread->start();
    

    There is a member function in Listen2Clients class like the following :-

    void Listening::recvFromQml(...){
     // Here I want to know whether the listening_thread is running or not
    }
    

    I cannot use "this->isRunning()" in Listening::recvFromQml().

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What exactly do you want to achieve? Why do you need to know where Listening is running at this place?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        What exactly do you want to achieve? Why do you need to know where Listening is running at this place?

        P Offline
        P Offline
        pingal
        wrote on last edited by pingal
        #3

        @Christian-Ehrlicher
        Listening::recvFromQml() is connected with a button on the app interface, everytime i press a button, i want the listening_thread to restart.
        But before it restarts, i need to know whether that thread is running or not

        Christian EhrlicherC KroMignonK 2 Replies Last reply
        0
        • P pingal

          @Christian-Ehrlicher
          Listening::recvFromQml() is connected with a button on the app interface, everytime i press a button, i want the listening_thread to restart.
          But before it restarts, i need to know whether that thread is running or not

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @pingal How should this work - Listen2Clients is in the running in this thread so it can't restart it's own thread. What exactly are you trying to solve with this?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • P pingal

            @Christian-Ehrlicher
            Listening::recvFromQml() is connected with a button on the app interface, everytime i press a button, i want the listening_thread to restart.
            But before it restarts, i need to know whether that thread is running or not

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @pingal said in Howto know whether the Qthread is running inside an object ?:

            Listening::recvFromQml() is connected with a button on the app interface, everytime i press a button, i want the listening_thread to restart.
            But before it restarts, i need to know whether that thread is running or not

            Do you mean:

            void Listening::recvFromQml(...){
               if(this->thread()->isRunning()) {
                   ...
               }
            }
            

            EDIT: but this looks to me like a very bad/error prone coding style

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pingal
              wrote on last edited by pingal
              #6

              you ppl are right, i'm doing it in the wrong way, let me clarify my objective.

              Listening::recvFromQml(...) receive information from QML and start processing in the listening_thread . I want to restart the whole process when new-information is received (i.e. restart the whole processes (thread) when Listening::recvFromQml(...) is invoked again)

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then (stop the old and) create the new thread inside recvFromQml().

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1

                • Login

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