Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. delete a QThread after processing
Forum Updated to NodeBB v4.3 + New Features

delete a QThread after processing

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 344 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    hi,

    I have this method in my class

     void showPreviews(QString path){        
            QFile file(path);
            if(!file.exists()){
                qDebug()<<"fichier introuvable..";
                return;
            }
            QThread *thread = QThread::create([=]{ preProcessFile(path);});
            thread->start();     
            return;
        }
    

    silly question : do i have to delete the thread and where ?
    should i connect finished() to deleteLater ?

    thank you

    ODБOïO KroMignonK 2 Replies Last reply
    0
    • ODБOïO ODБOï

      hi,

      I have this method in my class

       void showPreviews(QString path){        
              QFile file(path);
              if(!file.exists()){
                  qDebug()<<"fichier introuvable..";
                  return;
              }
              QThread *thread = QThread::create([=]{ preProcessFile(path);});
              thread->start();     
              return;
          }
      

      silly question : do i have to delete the thread and where ?
      should i connect finished() to deleteLater ?

      thank you

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2
        void showPreviews(QString path){
              qDebug()<<"loading the file for the preview" << path;
              QFile file(path);
              if(!file.exists()){
                  qDebug()<<"fichier introuvable..";
              }
      
              QThread *thread = QThread::create([=]{ preProcessFile(path);});
              QObject::connect(thread,&QThread::finished,thread,&QThread::deleteLater);
              QObject::connect(thread,&QThread::destroyed,[](){qDebug()<<"thread Deleted";});
              thread->start();
              return;
          }
      
      
      1 Reply Last reply
      1
      • ODБOïO ODБOï

        hi,

        I have this method in my class

         void showPreviews(QString path){        
                QFile file(path);
                if(!file.exists()){
                    qDebug()<<"fichier introuvable..";
                    return;
                }
                QThread *thread = QThread::create([=]{ preProcessFile(path);});
                thread->start();     
                return;
            }
        

        silly question : do i have to delete the thread and where ?
        should i connect finished() to deleteLater ?

        thank you

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #3

        @LeLev Yes, you have to delete the thread!

         void showPreviews(QString path){        
                QFile file(path);
                if(!file.exists()){
                    qDebug()<<"fichier introuvable..";
                    return;
                }
                QThread *thread = QThread::create([=]{ 
                    preProcessFile(path);
                    QThread::currentThread()->deleteLater();
                });
                thread->start();     
                return;
            }
        

        But I think, the easiest way to process a function in background is to use QtConncurrent::run() ==> https://doc.qt.io/qt-5/qtconcurrentrun.html#

         void showPreviews(QString path){        
                QFile file(path);
                if(!file.exists()){
                    qDebug()<<"fichier introuvable..";
                    return;
                }
                QtConcurrent::run([=]{ 
                    preProcessFile(path);
                });
                return;
            }
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        ODБOïO 1 Reply Last reply
        3
        • KroMignonK KroMignon

          @LeLev Yes, you have to delete the thread!

           void showPreviews(QString path){        
                  QFile file(path);
                  if(!file.exists()){
                      qDebug()<<"fichier introuvable..";
                      return;
                  }
                  QThread *thread = QThread::create([=]{ 
                      preProcessFile(path);
                      QThread::currentThread()->deleteLater();
                  });
                  thread->start();     
                  return;
              }
          

          But I think, the easiest way to process a function in background is to use QtConncurrent::run() ==> https://doc.qt.io/qt-5/qtconcurrentrun.html#

           void showPreviews(QString path){        
                  QFile file(path);
                  if(!file.exists()){
                      qDebug()<<"fichier introuvable..";
                      return;
                  }
                  QtConcurrent::run([=]{ 
                      preProcessFile(path);
                  });
                  return;
              }
          
          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @KroMignon thank you! I will try QtConncurrent::run() also, thank you for the suggerstion

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved