P@u1
1st June 2011, 16:02
Hello everyone,
I tried code similar to the following (i shortened it a bit):
class WorkerThread : public QThread
{
Q_OBJECT
public:
~WorkerThread();
protected:
void run();
private slots:
void update();
private:
volatile bool m_isInterrupted;
};
WorkerThread::~Workerthread()
{
m_isInterrupted = true;
wait();
}
void WorkerThread::run()
{
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(80);
exec();
}
void WorkerThread::update()
{
if(m_isInterrupted)
{
quit();
}
//do something
}
The wait() call is needed to prevent the main thread from deleting the workerThread object, while it is still running...
Note: I tested it with slightly different code, so I'm not 100% sure if the following statements are accurate, but I think so:
When closing the application, the process does not terminate and the quit() method is never called, which I verified by debugging.
I think this happens because when the mainthread calls wait() somehow the workingthread's event queue is not dispatched anymore, but I don't understand why, I thought the workingthread has a different evene queue.
It would be nice if you can explain me, why this happens and how I can fix it.
Alternative solutions for my problem would be nice, too!
Thanks for help in advance!
I tried code similar to the following (i shortened it a bit):
class WorkerThread : public QThread
{
Q_OBJECT
public:
~WorkerThread();
protected:
void run();
private slots:
void update();
private:
volatile bool m_isInterrupted;
};
WorkerThread::~Workerthread()
{
m_isInterrupted = true;
wait();
}
void WorkerThread::run()
{
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(80);
exec();
}
void WorkerThread::update()
{
if(m_isInterrupted)
{
quit();
}
//do something
}
The wait() call is needed to prevent the main thread from deleting the workerThread object, while it is still running...
Note: I tested it with slightly different code, so I'm not 100% sure if the following statements are accurate, but I think so:
When closing the application, the process does not terminate and the quit() method is never called, which I verified by debugging.
I think this happens because when the mainthread calls wait() somehow the workingthread's event queue is not dispatched anymore, but I don't understand why, I thought the workingthread has a different evene queue.
It would be nice if you can explain me, why this happens and how I can fix it.
Alternative solutions for my problem would be nice, too!
Thanks for help in advance!