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. How to stop a QThread inmediately?
Forum Updated to NodeBB v4.3 + New Features

How to stop a QThread inmediately?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 6.1k 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.
  • M Offline
    M Offline
    messi
    wrote on last edited by
    #2

    Hi
    how is about this
    http://www.qtcentre.org/threads/28957-How-to-stop-QThread

    or that
    http://stackoverflow.com/questions/19388207/how-to-stop-thread-qthread

    Do you have an event loop?

    Cheers J

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #3

      I had the same problem.
      waitForReadyRead(1000)
      and when CanFree (public boolean variable) is true the thread interrupts the loop and exit.

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Luis4984
        wrote on last edited by
        #4

        This is mi case, a litte code o mi program

        @
        class Transaction:public QThread
        {
        Q_OBJECT
        public:
        Transaction(QObject *parent):QThread(parent)
        {
        //Any initialization of data
        }
        protected:

        void run()
        {
        //Connection with serial port and socket connect (QtcpSocket.connectToHost()
        //QtcpSocket waitForConnect
        //Operation I/O blockink (Serial Port Read)
        while(true)
        {
        //Data analice and generation dato for server
        //Operation blocking QtcpWrite (waitForBytesWritten
        //Operation blocking, QtcpSocket (waitForReadyRead)
        //Data analice and generate date for serial port or finished
        if( fin)
        break;
        //Operation I/O blocking (serial Port write)
        //Operation I/O blockink (Serial Port Read)

        }

        }

        class MainMenu:public QWidget
        {
        Q_OBJECT
        Transaction transaction;
        MainMenu()
        {
        connect(ui.startTransaction,SIGNAL(clicked()),this,SLOT(start()));
        //Show a screen waiting connect(ui.stopTransaction,SIGNAL(clicked()),this,SLOT(stop()));
        }
        protected slots:
        void start()
        {
        transaction.start();
        }

        void start()
        {
        transaction.setTerminationEnabled(true);
        transaction.terminate();
        }

        }
        @

        under linux it works perfectly, under windows the application crash at runtime, the aplication dead. Whe append this secuencie.
        1- start
        2- stop while any operation blocking is apeend
        3- start
        The aplication crash in the call start() of metod QThread

        1 Reply Last reply
        0
        • mrdebugM Offline
          mrdebugM Offline
          mrdebug
          wrote on last edited by
          #5

          This is my standard approach to QThread in all platforms:
          @
          QThNetHTTPServerConn::QThNetHTTPServerConn() {
          CanFree= false;
          }

          QThNetHTTPServerConn::~QThNetHTTPServerConn() {
          CanFree= true;
          while (this->isRunning()) msleep(10);
          }

          void QThNetHTTPServerConn::run() {
          while (!CanFree) msleep(100);
          }
          @

          If you want to restart the same QThread please set CanFree= true; and then the QThread has ends the run function (use an event to know when) please run start() again.

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Luis4984
            wrote on last edited by
            #6

            But, it will not terminate inmediately. It will wait for all blocking operation on the socket and on the serial port until evaluate the condition. Consecuently the thread may be delay a lot of seconds.

            1 Reply Last reply
            0
            • mrdebugM Offline
              mrdebugM Offline
              mrdebug
              wrote on last edited by
              #7

              Is not possible, in Qt as Delphi or Freepascal.
              As is not possible to not freeze the main form if you click on a button that runs WaitForreadyRead()

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Luis4984
                wrote on last edited by
                #8

                then, whats is the solution on Qt?? Is the last solution do the I/O operations non blocking, implementing a state maching??

                1 Reply Last reply
                0
                • mrdebugM Offline
                  mrdebugM Offline
                  mrdebug
                  wrote on last edited by
                  #9

                  I use often QThread. When I have to stop it, I normally wait till the thread is stopped....
                  What are you doing exactly?

                  Need programmers to hire?
                  www.labcsp.com
                  www.denisgottardello.it
                  GMT+1
                  Skype: mrdebug

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Luis4984
                    wrote on last edited by
                    #10

                    Terminate the thread in any point, everywhere it is not important.
                    the problem is: I have only one hardware available, which is accesible by serial port, I want to stop the thread inmediately for leave the hardware free for other operation. Even if the user cancel the operation, and the operation in the serial port are blocking becouse it is more easy of managing.

                    1 Reply Last reply
                    0
                    • mrdebugM Offline
                      mrdebugM Offline
                      mrdebug
                      wrote on last edited by
                      #11

                      It is a typical situation for me.
                      In this case you have to verify frequently the CanFree variable in order to stop in less than a second and release the device.
                      Why you have to use the same device with more software?

                      Need programmers to hire?
                      www.labcsp.com
                      www.denisgottardello.it
                      GMT+1
                      Skype: mrdebug

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        Luis4984
                        wrote on last edited by
                        #12

                        No, I use the same device with the same software, but in diferents situacions, and diferents operations.
                        Maybe my English is no not enought precise, Sorry about that.

                        1 Reply Last reply
                        0
                        • mrdebugM Offline
                          mrdebugM Offline
                          mrdebug
                          wrote on last edited by
                          #13

                          If you use the a device with only one program you have to think to the QThread as a component that obscures the device and works as provider for the program.
                          You have to implement a public function SendCommand(), called from forms etc., the QThread has to run each operation and send a feedback to the program using signals.

                          Need programmers to hire?
                          www.labcsp.com
                          www.denisgottardello.it
                          GMT+1
                          Skype: mrdebug

                          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