PDA

View Full Version : QTimer in QThread working in ubuntu but not working in windows



pradeepreddyg95
23rd September 2012, 03:42
Qtimer timeout slot is executing in linux but not working in windows showing

QObject::startTimer: timers cannot be started from another thread

help me if anybody knows. Thanks in advance....

wysota
23rd September 2012, 10:13
Create the timer in the run() method of the thread.

pradeepreddyg95
24th September 2012, 02:56
hi wysota, I tried that method also same warning is coming but my confusion is same thing is working under ubuntu can u plz check the sample test code ...

Santosh Reddy
24th September 2012, 04:05
Two things you are missing

1. Event loop is not being ececuted. (i.e exec(), this is required, else the thread will exit)
2. Timer should started from the parent thread , i.e the ctor() and run() functions execute in different thread context hence the object created on ctor() should be moved the the the thread, or other way is to create the timer in run() method (as wysota said)

pradeepreddyg95
24th September 2012, 04:43
Thanks Santosh,
But in ubuntu i am using like this
void Thread::run()
{
while(1)
{
// some operations doing inside the loop
if(m_pProbeFailure[i].Value > m_nCuurrent_ADC_Value)
{
if(!m_pTimer[i].isActive())
m_pTimer[i].start();
}

}

}
like this iam using without calling exec() it working fine but in windows not working ... Plz solve my issue .... if any body ...

Santosh Reddy
24th September 2012, 05:27
QTimer will works when you have have a event loop (i.e. call exec());, or other way is to explictily process the events by calling QCoreApplication::processEvents(), from the local while loop.



void Thread::run()
{
while(1)
{
// some operations doing inside the loop
if(m_pProbeFailure[i].Value > m_nCuurrent_ADC_Value)
{
if(!m_pTimer[i].isActive())
m_pTimer[i].start();
}
QCoreApplication::processEvents();
}
}

pradeepreddyg95
24th September 2012, 06:27
Thanks Santosh its working ..... :cool:

Added after 16 minutes:

hi Santosh But iam unable to stop timers after timeout showing the following msg

void Thread::TimerTimeOut()
{
qDebug() << "TimerTimeOut " << sender()->objectName();

QTimer *pTimer = qobject_cast<QTimer*>(sender());
pTimer->stop();
}

TimerTimeOut "Timer1"
QObject::killTimer: timers cannot be stopped from another thread

ChrisW67
24th September 2012, 07:17
You cannot create the timer in one thread and operate it from another: there is no new error or problem here and the error message tells you exactly where the problem lies.

If the timer exists in the main thread then it can be started and stopped from code executing in the main thread only.
If the timer exists in the a secondary thread then it can be started and stopped from code executing in the secondary thread only.
Same goes for the third, fourth, fifth ...

You ask us to solve your problem when you have failed to take on board the information already supplied.

In your original code: code in the Thread class constructor executes in the main thread creating a timer in that thread. Code in the run() function runs in the controlled thread. Thus the pTimer is in one thread, and the pTimer->Start() is in another and never the two shall meet.