Hey! I've a class that is subclassing QThread and when I use a small sleep value like sleep(5) it works. If I do a sleep(1000) it doesn't even stop. Why is this?
Printable View
Hey! I've a class that is subclassing QThread and when I use a small sleep value like sleep(5) it works. If I do a sleep(1000) it doesn't even stop. Why is this?
Hi,
How are you trying to stop it, are you calling quit or exit? can you give a small piece, may be the run() part
I think stop here refers to sleep only (i.e. if sleep time passed is that much, the thread even does not sleeps) :)
it is like vineet.jain said. The thread does not sleep
Hi,
I tried running the same on QT simulator using QT SDK1.1.4 in ubuntu,
it works fine for me
#include <QtCore/QCoreApplication>
#include <QThread>
#include <QDebug>
class MyThread : public QThread {
public:
virtual void run();
};
void MyThread::run()
{
qDebug() << "I am here";
for( int count = 0; count < 20; count++ ) {
QThread::sleep(1000);
//QThread::sleep(10);
//sleep(1000);
qDebug() << "Ping!" ;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a1(argc, argv);
MyThread *a = new MyThread;
qDebug() << "I am here in main";
a->start();
a1.exec();
}
All the sleep methods including the commented ones run fine. That is the thread enters into long sleep mode as expected.
I am using Qt Symbian 4.6.3 ( sorry that i didn't mention that). The application logic is:
main -> Launches Thread A -> Launches Thread A1
[COLOR="white"]---------------------------------[/COLOR]-> Launches Thread A2
[COLOR="white"]---------------------------------[/COLOR]-> Launches Thread A3
My sleep code is on those Threads Ax
Are you sure the sleep(5) does work?
Yes it does work. I've tried to find the maximum value and I get for the first thread I launch is 2147. Then for the other threads keeps changing . Am I launching threads in a wrong way or what?
The current way of using QThreads is not to actually subclass this.
I know this seems contradictory, since most official resources still tell you to do so.
Please search for "Subclassing no longer recommended way of using QThread" in:
[url]http://developer.qt.nokia.com/doc/qt-4.7/qthread.html[/url]
Also: [url]http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/[/url]
Ok, I just wanted to launch a simple thread that would execute the run() method. Why do I need signals and connects? Do I have to emit a signal for my thread to invoque its run() method?
teemup thanks i was able to fix it by changing my threads implementation... and instead of a sleep i've used a QTimer to run my code.
That is a good solution and good thing you managed to get it working!