QThread - threads how to kill all of them?
-
Why not make re-usable objects ? And use e.g. QThreadPool ?
-
Do you mean like the one provided in the details of QThreadPool's documentation ?
-
If you have a fixed number of threads then just keep a list of them and stop them when needed.
if you make your worker object you can make it re-usuable so you only have to restart the thread.
-
@qDebug said in QThread - threads how to kill all of them?:
How do i keep "a list" and how can i use this list to stop the threads?
Instead of:
thread = new QThread; ... myThreads.append(thread->currentThread());
which is adding the controlling thread each time, add the child threads instead, like:
thread = new QThread; ... myThreads.append(thread);
Then you can do something like:
foreach (QThread * thread, myThreads) { thread->terminate(); }
Cheers.
-
@Paul-Colby Thanks you, that helps.
But in general i'm not sure if i use threads in the best known way yet. I got the thread, a worker class and inside the worker class i start a QFtp download. So all the worker classes show as child the QFtp. In my case i guess i have to stop the QFtp download. The download stops if i stop the worker. So far so good.
Not sure right now if i even need a worker class for QFtp. Can i just move the QFtp download to the thread?
So... do i loop through all the threads or the worker classes and call the abort function i've created to stop the QFtp download? I really can't find any examples about that particular problem.
Thanks!
-
@qDebug said in QThread - threads how to kill all of them?:
But in general i'm not sure if i use threads in the best known way yet. ... Not sure right now if i even need a worker class for QFtp.
I'm not at all familiar with QFtp, but a few notes that may help:
- QFtp is no longer exported in Qt5, so as per the Qt5 docs, I'd recommend you use the QNetworkAccessManager class directly if that's possible for you.
- with both classes, it should be possible to do the download asynchronously without using any threads. The only reason I'd use worker threads here, is if you had to do any real processing of the data as it was arriving, or you have some slick UI animation going on and don't want any stutters / artefacts.
- if you can use QNetworkAccessManager, then have a look at either Network Download Example or Network Download Manager Example, neither of which require threads.
- otherwise check out the (older) FTP Example, which also doesn't require threads.
Good luck :)
-
@qDebug said in QThread - threads how to kill all of them?:
But in general i'm not sure if i use threads in the best known way yet.
If you need to call
QThread::terminate
then you're doing something very wrong. I like to say that this (doiung forceful termination) is the equivalent of hitting your girl with baseball bat after having a nice dinner. :) -
After a lot of try and error, mostly error and crashes, i ended up with this working solution:
foreach(FTPDownload* w, g_Worker) { if(w->_working) { w->abort(); // sets _working = false and _abort = true w->ftp->deleteLater(); // ends the QFtp download w->thread()->quit(); w->thread()->deleteLater(); } }
The only problem left is when i remove the table from the QTableView the progress update wont terminate and the app crashes, because the table does not exist anymore.
I guess i have to add the the function that executes the code from above somehow in a event loop so the code after that deletes the table wont be executed before all the downloads are really stopped. Right?