stefanadelbert
6th April 2011, 05:30
I have MyService (derived from QtService<QCoreApplication>) which has a MyThread (derived from QThread). MyThread::run() creates a QTcpServer and called exec(). I'm on Windows 7 using Qt 4.6.2.
In MyService::start() I call MyThread::start(). QThread::started() is emitted by MyThread.
In MyService::stop() I call MyThread::quit(). I would expect QThread::finished() to be emitted by MyThread, but it isn't.
If I call MyThread::terminate() from MyService::stop(), I get both QThread:finished() and QThread:terminated().
Should I be expecting QThread:finished() to be emitted? The documentation says the following:
void QThread::finished () [signal]
This signal is emitted when the thread has finished executing.
See also started() and terminated().
Maybe the thread only emits QThread::finished() if it finishes of its own accord, i.e. not by having its quit() or exit() functions called.
Added after 10 minutes:
Interesting...
I have just commented out the call to exec() from MyThread::run(). MyThread::run() returns straight away (it doesn't do anything now) and QThread::finished() is emitted.
I take from this that one should not expect to get QThread::finished() when calling QThread::quit() or QThread::exit(). QThread::finished() is only emitted if the thread finishes of its own accord or QThread::terminate() is called. If QThread::terminate() is called, both QThread::finished() and QThread::terminated() are emitted.
This does make sense. If you're calling QThread::quit() or QThread::exit() explicitly, you're assuming control and shouldn't need the thread to tell you that it's finished because you're telling it to finish.
Anyone got anything to add?
In MyService::start() I call MyThread::start(). QThread::started() is emitted by MyThread.
In MyService::stop() I call MyThread::quit(). I would expect QThread::finished() to be emitted by MyThread, but it isn't.
If I call MyThread::terminate() from MyService::stop(), I get both QThread:finished() and QThread:terminated().
Should I be expecting QThread:finished() to be emitted? The documentation says the following:
void QThread::finished () [signal]
This signal is emitted when the thread has finished executing.
See also started() and terminated().
Maybe the thread only emits QThread::finished() if it finishes of its own accord, i.e. not by having its quit() or exit() functions called.
Added after 10 minutes:
Interesting...
I have just commented out the call to exec() from MyThread::run(). MyThread::run() returns straight away (it doesn't do anything now) and QThread::finished() is emitted.
I take from this that one should not expect to get QThread::finished() when calling QThread::quit() or QThread::exit(). QThread::finished() is only emitted if the thread finishes of its own accord or QThread::terminate() is called. If QThread::terminate() is called, both QThread::finished() and QThread::terminated() are emitted.
This does make sense. If you're calling QThread::quit() or QThread::exit() explicitly, you're assuming control and shouldn't need the thread to tell you that it's finished because you're telling it to finish.
Anyone got anything to add?