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. Problem with running timer in another thread
Qt 6.11 is out! See what's new in the release blog

Problem with running timer in another thread

Scheduled Pinned Locked Moved Solved General and Desktop
44 Posts 9 Posters 18.7k Views 3 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.
  • D Damian7546

    To my project I added protocol implementation published in below:
    [https://github.com/ashaduri/qt-cctalk/tree/main/cctalk](link url)

    I not use included test_gui only added to my class.

    I do not understand why when the polling timer try start I get error :QObject::startTimer: Timers cannot be started from another thread
    I am talking about 78 line in below file:
    [https://github.com/ashaduri/qt-cctalk/blob/main/cctalk/cctalk_device.cpp](link url)

    Anyone can help me how run this timer ?

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by
    #3

    @Damian7546 said in Problem with running timer in another thread:

    Anyone can help me how run this timer ?

    Allocate the timer on the heap and make it a child of the class it is in.
    Then it will change its thread-affinity with the parent.


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    D JoeCFDJ 2 Replies Last reply
    1
    • SGaistS SGaist

      Hi,

      Your timer is a class member and is thus created in the thread where your object is created in and not moved to the new thread.

      D Offline
      D Offline
      Damian7546
      wrote on last edited by
      #4

      @SGaist
      So how solution this problem ? I try use moveToThread no result

      1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @Damian7546 said in Problem with running timer in another thread:

        Anyone can help me how run this timer ?

        Allocate the timer on the heap and make it a child of the class it is in.
        Then it will change its thread-affinity with the parent.

        D Offline
        D Offline
        Damian7546
        wrote on last edited by
        #5

        @Pl45m4 I should change only event_timer_ or whole function CctalkDevice::startTimer() ?
        Can you explain me?

        Pl45m4P 1 Reply Last reply
        0
        • D Damian7546

          @Pl45m4 I should change only event_timer_ or whole function CctalkDevice::startTimer() ?
          Can you explain me?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #6

          @Damian7546 said in Problem with running timer in another thread:

          I should change only event_timer_ or whole function CctalkDevice::startTimer() ?

          @Pl45m4 said in Problem with running timer in another thread:

          Allocate the timer on the heap and make it a child of the class it is in.

          This is everything you have to do and the error/warning should be gone.
          No need to change the function.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          D 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Damian7546 said in Problem with running timer in another thread:

            I should change only event_timer_ or whole function CctalkDevice::startTimer() ?

            @Pl45m4 said in Problem with running timer in another thread:

            Allocate the timer on the heap and make it a child of the class it is in.

            This is everything you have to do and the error/warning should be gone.
            No need to change the function.

            D Offline
            D Offline
            Damian7546
            wrote on last edited by Damian7546
            #7

            @Pl45m4 In cctalk_device.h I changed

            QTimer event_timer_;
            

            to

            QTimer *event_timer_; ,
            

            next in CctalkDevice() constructor I added

            event_timer_ = new QTimer(this);
            

            and enought ?

            Pl45m4P JoeCFDJ jsulmJ 3 Replies Last reply
            0
            • D Damian7546

              @Pl45m4 In cctalk_device.h I changed

              QTimer event_timer_;
              

              to

              QTimer *event_timer_; ,
              

              next in CctalkDevice() constructor I added

              event_timer_ = new QTimer(this);
              

              and enought ?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #8

              @Damian7546 said in Problem with running timer in another thread:

              and enough ?

              When there is no more error, yes.

              Does it work now?


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              D 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @Damian7546 said in Problem with running timer in another thread:

                and enough ?

                When there is no more error, yes.

                Does it work now?

                D Offline
                D Offline
                Damian7546
                wrote on last edited by
                #9

                @Pl45m4 No, it still the same error : QObject::startTimer: Timers cannot be started from another thread

                1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @Damian7546 said in Problem with running timer in another thread:

                  Anyone can help me how run this timer ?

                  Allocate the timer on the heap and make it a child of the class it is in.
                  Then it will change its thread-affinity with the parent.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #10

                  @Pl45m4 Allocating the timer on the heap or stack should not matter.

                  Pl45m4P 1 Reply Last reply
                  0
                  • D Damian7546

                    @Pl45m4 In cctalk_device.h I changed

                    QTimer event_timer_;
                    

                    to

                    QTimer *event_timer_; ,
                    

                    next in CctalkDevice() constructor I added

                    event_timer_ = new QTimer(this);
                    

                    and enought ?

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #11

                    @Damian7546 Maybe try to add the following code into the func

                        if ( QThread::currentThread() != thread() ) {
                            metaObject()->invokeMethod( this, "startTimer()", Qt::QueuedConnection );
                            return;
                        }
                    

                    Make startTimer() to be a slot.

                    D 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @Pl45m4 Allocating the timer on the heap or stack should not matter.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #12

                      @JoeCFD

                      I know :)
                      Haven't checked the complete repository where and how everything is put together.

                      But yeah,

                      event_timer.setParent(this);
                      

                      might also work.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      SGaistS D 2 Replies Last reply
                      0
                      • Pl45m4P Pl45m4

                        @JoeCFD

                        I know :)
                        Haven't checked the complete repository where and how everything is put together.

                        But yeah,

                        event_timer.setParent(this);
                        

                        might also work.

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Pl45m4 that's a bad idea: this will trigger a double deletion. One when the parent is deleted and the second when the object goes out of scope.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        Pl45m4P 1 Reply Last reply
                        2
                        • D Damian7546

                          @Pl45m4 In cctalk_device.h I changed

                          QTimer event_timer_;
                          

                          to

                          QTimer *event_timer_; ,
                          

                          next in CctalkDevice() constructor I added

                          event_timer_ = new QTimer(this);
                          

                          and enought ?

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @Damian7546 said in Problem with running timer in another thread:

                          next in CctalkDevice() constructor I added

                          Don't do this in the constructor, do it in the method which is actually executed in the other thread.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1
                          • JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #15

                            As @jsulm says, reminder: objects created in thread constructor live in parent thread, objects created in thread run live in that thread.

                            1 Reply Last reply
                            0
                            • SGaistS SGaist

                              @Pl45m4 that's a bad idea: this will trigger a double deletion. One when the parent is deleted and the second when the object goes out of scope.

                              Pl45m4P Offline
                              Pl45m4P Offline
                              Pl45m4
                              wrote on last edited by Pl45m4
                              #16

                              @SGaist said in Problem with running timer in another thread:

                              that's a bad idea: this will trigger a double deletion

                              Oh my... that's why I initially thought of heap allocation and then using the parent-child architecture :D

                              @jsulm said in Problem with running timer in another thread:

                              Don't do this in the constructor, do it in the method which is actually executed in the other thread.

                              But when creating the timer on the heap and as child of the containing class in the contructor... if you then move the ccDevice object, the timer (= child of ccDevice) is also moved, right?!

                              @JoeCFD said in Problem with running timer in another thread:

                              Allocating the timer on the heap or stack should not matter.

                              So this doesn't matter in technical terms as the timer can/should be allocated on the stack, however when you want to use the parent-child relation to move the object to another thread, it does matter to avoid double deletion


                              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                              ~E. W. Dijkstra

                              1 Reply Last reply
                              0
                              • Pl45m4P Pl45m4

                                @JoeCFD

                                I know :)
                                Haven't checked the complete repository where and how everything is put together.

                                But yeah,

                                event_timer.setParent(this);
                                

                                might also work.

                                D Offline
                                D Offline
                                Damian7546
                                wrote on last edited by
                                #17

                                @Pl45m4 It doesn't work, still the same.

                                1 Reply Last reply
                                0
                                • JoeCFDJ JoeCFD

                                  @Damian7546 Maybe try to add the following code into the func

                                      if ( QThread::currentThread() != thread() ) {
                                          metaObject()->invokeMethod( this, "startTimer()", Qt::QueuedConnection );
                                          return;
                                      }
                                  

                                  Make startTimer() to be a slot.

                                  D Offline
                                  D Offline
                                  Damian7546
                                  wrote on last edited by
                                  #18

                                  @JoeCFD said in Problem with running timer in another thread:

                                  @Damian7546 Maybe try to add the following code into the func

                                      if ( QThread::currentThread() != thread() ) {
                                          metaObject()->invokeMethod( this, "startTimer()", Qt::QueuedConnection );
                                          return;
                                      }
                                  

                                  Make startTimer() to be a slot.

                                  This one , still not working
                                  QMetaObject::invokeMethod: No such method qtcc::CctalkDevice::startTimer()()

                                  1 Reply Last reply
                                  0
                                  • hskoglundH Offline
                                    hskoglundH Offline
                                    hskoglund
                                    wrote on last edited by
                                    #19

                                    Hi, try without the parentheses, like this:
                                    ...
                                    metaObject()->invokeMethod( this, "startTimer", Qt::QueuedConnection );
                                    ...

                                    D 1 Reply Last reply
                                    1
                                    • hskoglundH hskoglund

                                      Hi, try without the parentheses, like this:
                                      ...
                                      metaObject()->invokeMethod( this, "startTimer", Qt::QueuedConnection );
                                      ...

                                      D Offline
                                      D Offline
                                      Damian7546
                                      wrote on last edited by Damian7546
                                      #20

                                      @hskoglund

                                      still the same QMetaObject::invokeMethod: No such method qtcc::CctalkDevice::startTimer()

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        Damian7546
                                        wrote on last edited by
                                        #21

                                        I thinking why we are talking about more than one thread? And why the error applies other thread? Since both CctalkDevice and MyCcTalkApp class are the same thread.... Only SerialWorker works in own thread which one is launched by CctalkLinkController .
                                        The polling timer is in CctalkDevice, so what is the problem ?

                                        JoeCFDJ 1 Reply Last reply
                                        0
                                        • D Damian7546

                                          I thinking why we are talking about more than one thread? And why the error applies other thread? Since both CctalkDevice and MyCcTalkApp class are the same thread.... Only SerialWorker works in own thread which one is launched by CctalkLinkController .
                                          The polling timer is in CctalkDevice, so what is the problem ?

                                          JoeCFDJ Offline
                                          JoeCFDJ Offline
                                          JoeCFD
                                          wrote on last edited by JoeCFD
                                          #22

                                          @Damian7546 print out thread ids on the screen in the constructor and in this function. Check out why they are different.

                                          D 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