title: Qt timers cannot be stopped from another thread
body: <p>Hy,</p>
  
  <p>I'm writing my first Qt program and getting now in troubles with:</p>
  
  <p><strong>QObject::killTimer: timers cannot be stopped from another thread</strong></p>
  
  <p><strong>QObject::startTimer: timers cannot be started from another thread</strong></p>
  
  <p>My program will communicate to a CANOpen bus for that I'm using the <a href="http://www.canfestival.org">Canfestival Stack</a>. The Canfestival will work with callback methods. To detects timeout in communication I setup a timer function (somehow like a watchdog). My timer package consist out of a "tmr" module, a "TimerForFWUpgrade" module and a "SingleTimer" module. The "tmr" module was originally C programmed so the static "TimerForFWUpgrade" methods will interface it. The "tmr" module will be part of a C programed Firmware update package.</p>
  
  <p>The timer will work as follows. Before a message is sent I will call TMR_Set method. An then in my idle program loop  with TMR_IsElapsed we check for a timer underflow. If TMR_IsElapsed I will do the errorhandling. As you see the TMR_Set method will be called continuously and restart the QTimer again and again.</p>
  
  <p>The above noted errors are appearing if I start my program. Can you tell me if my concept could work? Why does this errors appear? Do I have to use additional threads (QThread) to the main thread? </p>
  
  <p>Thank you</p>
  
  <p>Matt</p>
  
  <p><em>Run and Idle loop:</em></p>
  
  <pre><code>void run
  {
      // start communicate with callbacks where TMR_Set is set continously
      ...
  
      while(TMR_IsElapsed(TMR_NBR_CFU) != 1);
  
      // if TMR_IsElapsed check for errorhandling
      ....
  }  
  </code></pre>
  
  <p><em>Module tmr (interface to C program):</em></p>
  
  <pre><code>extern "C"
  {
  void TMR_Set(UINT8 tmrnbr, UINT32 time)
  {
      TimerForFWUpgrade::set(tmrnbr, time);
  }
  
  INT8 TMR_IsElapsed(UINT8 tmrnbr)
  {
   return TimerForFWUpgrade::isElapsed(tmrnbr);
  }
  }
  </code></pre>
  
  <p><em>Module TimerForFWUpgrade:</em></p>
  
  <pre><code>SingleTimer* TimerForFWUpgrade::singleTimer[NR_OF_TIMERS];
  
  TimerForFWUpgrade::TimerForFWUpgrade(QObject* parent)
  {
  
      for(unsigned char i = 0; i &lt; NR_OF_TIMERS; i++)
      {
          singleTimer[i] = new SingleTimer(parent);
      }
  }
  
  //static
  void TimerForFWUpgrade::set(unsigned char tmrnbr, unsigned int time)
  {
      if(tmrnbr &lt; NR_OF_TIMERS)
      {
          time *= TimerForFWUpgrade::timeBase;
          singleTimer[tmrnbr]-&gt;set(time);
      }
  
  }
  
  
  //static
  char TimerForFWUpgrade::isElapsed(unsigned char tmrnbr)
  {
      if(true == singleTimer[tmrnbr]-&gt;isElapsed())
      {
          return 1;
      }
      else
      {
          return 0;
      }
  }
  </code></pre>
  
  <p><em>Module SingleTimer:</em></p>
  
  <pre><code>SingleTimer::SingleTimer(QObject* parent) : QObject(parent),
                                              pTime(new QTimer(this)),
                                              myElapsed(true)
  {
      connect(pTime, SIGNAL(timeout()), this, SLOT(slot_setElapsed()));
      pTime-&gt;setTimerType(Qt::PreciseTimer);
      pTime-&gt;setSingleShot(true);
  }
  
  void SingleTimer::set(unsigned int time)
  {
      myElapsed = false;
      pTime-&gt;start(time);
  }
  
  bool SingleTimer::isElapsed()
  {
      QCoreApplication::processEvents();
      return myElapsed;
  }
  
  void SingleTimer::slot_setElapsed()
  {
      myElapsed = true;
  }
  </code></pre>