
Originally Posted by
wysota
If you want to do idle processing in the thread besides the main task the thread is doing, use a timer instead of sleep().
In the initial post of the op, I understood the idle processing is the main task. (Polling the device is the only task of the thread, and it must be done at idle time.) In that case, is its timer-based solution (re-coded below) appropriate?
class DevicePoller
: public QThread{
Q_OBJECT
slots:
void poll();
protected:
virtual void run()
{
connect( m_timer, SIGNAL( timeout() ), this, SLOT( poll() ) );
m_timer.start( 0 );
exec();
}
};
class DevicePoller : public QThread
{
Q_OBJECT
QTimer m_timer;
slots:
void poll();
protected:
virtual void run()
{
connect( m_timer, SIGNAL( timeout() ), this, SLOT( poll() ) );
m_timer.start( 0 );
exec();
}
};
To copy to clipboard, switch view to plain text mode
Or, is it best to avoid timers at all, and launch a thread with QThread::IdlePriority:
class DevicePoller
: public QThread{
protected:
virtual void run()
{
while( !finished ) poll();
}
};
DevicePoller mythread;
mythread.
start( QThread::IdlePriority );
// ...
class DevicePoller : public QThread
{
protected:
virtual void run()
{
while( !finished ) poll();
}
};
DevicePoller mythread;
mythread.start( QThread::IdlePriority );
// ...
To copy to clipboard, switch view to plain text mode
Or, am I wrong and there is another strategy?
Thanks for the rapid answer anyway!
Bookmarks