You can use terminate() but nobody recommends doing that.
What you should do is intersperse a control variable throughout your job - and break your job into small parts.
So, when you sub-class QThread you should make a private variable :
bool keepRunning;
and rewrite stop() to include keepRunning = false;
then your run() looks like this...
run()
{
keepRunning = true;
//do some stuff
if(!keepRunning) return;
//do more stuff
if(!keepRunning) return;
//do even more stuff
//etc
}
run()
{
keepRunning = true;
//do some stuff
if(!keepRunning) return;
//do more stuff
if(!keepRunning) return;
//do even more stuff
//etc
}
To copy to clipboard, switch view to plain text mode
here's my stop():
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/// wrapper for stopping the thread gracefully
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool BaseDevice::stop (unsigned long time )
{
keepRunning = false;
return QThread::wait(time);
//returns false if timedout }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/// wrapper for stopping the thread gracefully
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool BaseDevice::stop (unsigned long time )
{
keepRunning = false;
QThread::quit();
return QThread::wait(time);//returns false if timedout
}
To copy to clipboard, switch view to plain text mode
Bookmarks