How to restart a thread?
-
How to properly restart a QThread? I have been googling this four hours and can't find any concrete answer.
What I have is the basic
moveToThreadimplementation:VideoHandler::VideoHandler() { VideoBurner *videoBurner = new VideoBurner; videoBurner->moveToThread(&videoBurnerThread); connect(this, &VideoHandler::signalVideoData, videoBurner, &VideoBurner::setVideoData); connect(this, &VideoHandler::signalVideoBurn, videoBurner, &VideoBurner::initVideoBurn); // don't really want it since there is only 1 object //connect(&videoBurnerThread, &QThread::finished, videoBurner, &QObject::deleteLater); connect(videoBurner, &VideoBurner::burnProcess, this, [=](QString step, int percentage){ emit burningProcess(step, percentage); }); }This is the declaration of my Class:
class VideoHandler : public QObject { Q_OBJECT QThread videoBurnerThread; ... bunch of defintions ... }The
VideoBurnerclass is also a subclass ofQObjectclass VideoBurner : public QObject { Q_OBJECT ... bunch of definitions ... }For the first run this works perfect, signals and slots all work.
The issue happens when I want to "cancel" the thread.
For example it is working on some "heavy" calculation that I want to stop from the main thread so I call:videoBurnerThread.quit();with acancelfunction fromVideoHandler.I don't
wait()on it as it would block the main thread.quit()triggers&QThread::finished, but I commented out that connection as it destroys my only object instance.(also I can't find any example that creates an object and puts it on a thread outside a constructor, if that's even possible? when I tried the app would just close without errors)
I found that the
quit()only stops the event loop, not doing anything besides that, which leads to the issue ofrestartingthe tread.After I want to do the "heavy" stuff again, and I call
videoBurnerThread.start();to start it again, I get results of last performed function fromVideoBurnerand somesignalsfrom it, before it actually starts doing what it should.Can I somehow stop whatever it was working on and just restart it? I tried sending an "abort" signal from
VideoHandlerso it doesn't emit signals, but that didn't work.Do I absolutely need the
&QObject::deleteLaterso it stops? If so, how to reinitializeVideoBurnercorrectly inside some function and reconnect it's signal/slot?Also there is no logic in
VideoBurnerdestructor just apublic: virtual ~VideoBurner(){};, could this be an issue? -
How to properly restart a QThread? I have been googling this four hours and can't find any concrete answer.
What I have is the basic
moveToThreadimplementation:VideoHandler::VideoHandler() { VideoBurner *videoBurner = new VideoBurner; videoBurner->moveToThread(&videoBurnerThread); connect(this, &VideoHandler::signalVideoData, videoBurner, &VideoBurner::setVideoData); connect(this, &VideoHandler::signalVideoBurn, videoBurner, &VideoBurner::initVideoBurn); // don't really want it since there is only 1 object //connect(&videoBurnerThread, &QThread::finished, videoBurner, &QObject::deleteLater); connect(videoBurner, &VideoBurner::burnProcess, this, [=](QString step, int percentage){ emit burningProcess(step, percentage); }); }This is the declaration of my Class:
class VideoHandler : public QObject { Q_OBJECT QThread videoBurnerThread; ... bunch of defintions ... }The
VideoBurnerclass is also a subclass ofQObjectclass VideoBurner : public QObject { Q_OBJECT ... bunch of definitions ... }For the first run this works perfect, signals and slots all work.
The issue happens when I want to "cancel" the thread.
For example it is working on some "heavy" calculation that I want to stop from the main thread so I call:videoBurnerThread.quit();with acancelfunction fromVideoHandler.I don't
wait()on it as it would block the main thread.quit()triggers&QThread::finished, but I commented out that connection as it destroys my only object instance.(also I can't find any example that creates an object and puts it on a thread outside a constructor, if that's even possible? when I tried the app would just close without errors)
I found that the
quit()only stops the event loop, not doing anything besides that, which leads to the issue ofrestartingthe tread.After I want to do the "heavy" stuff again, and I call
videoBurnerThread.start();to start it again, I get results of last performed function fromVideoBurnerand somesignalsfrom it, before it actually starts doing what it should.Can I somehow stop whatever it was working on and just restart it? I tried sending an "abort" signal from
VideoHandlerso it doesn't emit signals, but that didn't work.Do I absolutely need the
&QObject::deleteLaterso it stops? If so, how to reinitializeVideoBurnercorrectly inside some function and reconnect it's signal/slot?Also there is no logic in
VideoBurnerdestructor just apublic: virtual ~VideoBurner(){};, could this be an issue?@Wiru said in How to restart a thread?:
For example it is working on some "heavy" calculation that I want to stop from the main thread so I call:
videoBurnerThread.quit();with acancelfunction fromVideoHandler.If your
VideoBurnerobject is in the middle of a calculation and you quit the event loop, yourVideoBurnerwill enter "suspended animation". It will remain stuck in its half-calculated state... and then it will continue the calculation when the event loop is restarted.To avoid that, you must get your
VideoBurnerobject to discard its incomplete calculations when you want to quit. -
@Wiru said in How to restart a thread?:
For example it is working on some "heavy" calculation that I want to stop from the main thread so I call:
videoBurnerThread.quit();with acancelfunction fromVideoHandler.If your
VideoBurnerobject is in the middle of a calculation and you quit the event loop, yourVideoBurnerwill enter "suspended animation". It will remain stuck in its half-calculated state... and then it will continue the calculation when the event loop is restarted.To avoid that, you must get your
VideoBurnerobject to discard its incomplete calculations when you want to quit.@JKSH said in How to restart a thread?:
If your VideoBurner object is in the middle of a calculation and you quit the event loop, your VideoBurner will enter "suspended animation". It will remain stuck in its half-calculated state... and then it will continue the calculation when the event loop is restarted.
To avoid that, you must get your VideoBurner object to discard its incomplete calculations when you want to quit.Thank you for the info, I'll search for how to achieve this.