QUdpSocket HeartBeat behaviour [SOLVED]
-
Good idea to use QThread and sleep there in a while loop.
Or you can use QTimer to trigger some slot every N time value you want... -
You can use a QTimer inside a QThread loop like this :
@
void Thread::run()
{
QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(sendStatu()));
timer.setInterval(5);
timer.start();exec();
timer.stop();
}void Thread::sendStatu()
{
}
@Important : when using signal and slots inside the thread run() function you must add exec(); This is because they need an event loop to be executed!
-
Only one drawback: it works uses signal/slot connection, but it is also an advantage: No extra loops and triggered exactly when you need it... nothing more...
-
You can create a normal QObject that does the timing and writing to the socket, then use "QObject::moveToThread()":http://qt-project.org/doc/qt-4.8/qobject.html#moveToThread to have it run in that thread.
A good example of how to do this is "this post":http://codethis.wordpress.com/2011/04/04/using-qthread-without-subclassing/ -
Acctually you shouldn't subclass QThread if possible. Have a look at "this wiki entry":http://qt-project.org/wiki/QThreads_general_usage for a best practise description concerning QThreads.
Another option is QRunnable but in your case its not really feasable since thats more for a one time fire and forget sort of approach.