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. Program freeze when try to get MD5 of files

Program freeze when try to get MD5 of files

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 6 Posters 2.7k 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.
  • S Offline
    S Offline
    saeid0034
    wrote on last edited by
    #14

    its fully fixed by adding qApp->processEvents(); to MD5 while

    aha_1980A 1 Reply Last reply
    -1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #15

      You do realise that your code is wrong in terms of offloading the work ? As I already wrote: result will be blocking if the computation is not finished.

      The idea is to use the watcher so you let the computation run and in the slot connected to your QFutureWatcher you run your UI updates.

      What you have here is mostly the same code as before, but more complicated, since you are blocking your event loop with your calls to result.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      4
      • S saeid0034

        its fully fixed by adding qApp->processEvents(); to MD5 while

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #16

        Hi @saeid0034,

        its fully fixed by adding qApp->processEvents(); to MD5 while

        For sure not! Please don't use processEvents() if you don't know what you are doing!

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        4
        • SGaistS SGaist

          You do realise that your code is wrong in terms of offloading the work ? As I already wrote: result will be blocking if the computation is not finished.

          The idea is to use the watcher so you let the computation run and in the slot connected to your QFutureWatcher you run your UI updates.

          What you have here is mostly the same code as before, but more complicated, since you are blocking your event loop with your calls to result.

          S Offline
          S Offline
          saeid0034
          wrote on last edited by
          #17

          @SGaist i don't know how correctly use QFutureWatcher in my code...
          As you can see in my full cod in above im add QFutureWatcher to my cod, is it wrong?
          Can you please guide me to how do this?

          aha_1980A 1 Reply Last reply
          0
          • S saeid0034

            @SGaist i don't know how correctly use QFutureWatcher in my code...
            As you can see in my full cod in above im add QFutureWatcher to my cod, is it wrong?
            Can you please guide me to how do this?

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #18

            Hi @saeid0034,

            As @SGaist already said, calling result() when the future is not done yet will block your event loop.

            You should call result() in the slot connected to finished().

            Regards

            Qt has to stay free or it will die.

            S 1 Reply Last reply
            3
            • aha_1980A aha_1980

              Hi @saeid0034,

              As @SGaist already said, calling result() when the future is not done yet will block your event loop.

              You should call result() in the slot connected to finished().

              Regards

              S Offline
              S Offline
              saeid0034
              wrote on last edited by
              #19

              @aha_1980 thanks for information
              problem is i dont know much about working with slots and QFutureWatcher
              can you please say to me i need to do what with this code?

              QString Md5_gen(QString const &s)
              {
                  QString pakchunk_Md5;
                  QCryptographicHash crypto(QCryptographicHash::Md5);
                  QFile pakchunk(QDir::currentPath() + s);
                  if (pakchunk.open(QIODevice::ReadOnly))
                  {
                      while(!pakchunk.atEnd()){
                      crypto.addData(pakchunk.read(8192));
                      }
                  } else
                  {
                      qDebug() << "Can't open file.";
                      pakchunk_Md5 = "nofile";
                      return pakchunk_Md5;
                  }
                  pakchunk_Md5 = crypto.result().toHex();
                  return pakchunk_Md5;
              }
              
              MainWindow2::MainWindow2(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow2)
              {
                  ui->setupUi(this);
              }
              
              MainWindow2::~MainWindow2()
              {
                  delete ui;
              }
              
              void MainWindow2::on_pushButton_clicked()
              {
                  int file_ok = 0;
              
                  QFutureWatcher<void>* watcher = new QFutureWatcher<void>();
                  connect(watcher, SIGNAL(finished()), this, SLOT(Md5_gen()));
                  //file pak33
                  QString loc33 = "/SM/test.pak";
                  QFuture<QString> future33 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc33);
                  QString pakchunk33 = future33.result();
                  watcher->setFuture(future33);
                  qDebug() << pakchunk33;
                  if (pakchunk33 == "f7002d4419cd235a87746715ba6fb2ea")
                  {
                      qDebug() << "OK";
                      file_ok++;
                      ui->label_8->setText("OK");
                      ui->label_8->setStyleSheet("QLabel { color : green; }");
                  } else if (pakchunk33 == "nofile")
                  {
                      qDebug() << "no file found";
                      ui->label_8->setText("not found");
                      ui->label_8->setStyleSheet("QLabel { color : red; }");
                  } else
                  {
                      qDebug() << "file is diffrent";
                      ui->label_8->setText("wrong");
                      ui->label_8->setStyleSheet("QLabel { color : red; }");
                  }
              
                  //file pak34
                  QString loc34 = "/SM/test2.pak";
                  QFuture<QString> future34 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc34);
                  QString pakchunk34 = future34.result();
                  qDebug() << pakchunk34;
                  watcher->setFuture(future34);
                  if (pakchunk34 == "64c77598586b6c3cb60beed0b0750620")
                  {
                      qDebug() << "OK";
                      file_ok++;
                      ui->label->setText("OK");
                      ui->label->setStyleSheet("QLabel { color : green; }");
                  } else if (pakchunk34 == "nofile")
                  {
                      qDebug() << "no file found";
                      ui->label->setText("not found");
                      ui->label->setStyleSheet("QLabel { color : red; }");
                  } else
                  {
                      qDebug() << "file is diffrent";
                      ui->label->setText("wrong");
                      ui->label->setStyleSheet("QLabel { color : red; }");
                  }
              
                  //file pak35
                  QString loc35 = "/SM/test3.pak";
                  QFuture<QString> future35 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc35);
                  QString pakchunk35 = future35.result();
                  watcher->setFuture(future35);
                  qDebug() << pakchunk35;
                  if (pakchunk35 == "ee53f7a7656a32b5278c460baec46f16")
                  {
                      qDebug() << "OK";
                      file_ok++;
                      ui->label_7->setText("OK");
                      ui->label_7->setStyleSheet("QLabel { color : green; }");
                  } else if (pakchunk35 == "nofile")
                  {
                      qDebug() << "no file found";
                      ui->label_7->setText("not found");
                      ui->label_7->setStyleSheet("QLabel { color : red; }");
                  } else
                  {
                      qDebug() << "file is diffrent";
                      ui->label_7->setText("wrong");
                      ui->label_7->setStyleSheet("QLabel { color : red; }");
                  }
              }
              
              aha_1980A 1 Reply Last reply
              0
              • S saeid0034

                @aha_1980 thanks for information
                problem is i dont know much about working with slots and QFutureWatcher
                can you please say to me i need to do what with this code?

                QString Md5_gen(QString const &s)
                {
                    QString pakchunk_Md5;
                    QCryptographicHash crypto(QCryptographicHash::Md5);
                    QFile pakchunk(QDir::currentPath() + s);
                    if (pakchunk.open(QIODevice::ReadOnly))
                    {
                        while(!pakchunk.atEnd()){
                        crypto.addData(pakchunk.read(8192));
                        }
                    } else
                    {
                        qDebug() << "Can't open file.";
                        pakchunk_Md5 = "nofile";
                        return pakchunk_Md5;
                    }
                    pakchunk_Md5 = crypto.result().toHex();
                    return pakchunk_Md5;
                }
                
                MainWindow2::MainWindow2(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow2)
                {
                    ui->setupUi(this);
                }
                
                MainWindow2::~MainWindow2()
                {
                    delete ui;
                }
                
                void MainWindow2::on_pushButton_clicked()
                {
                    int file_ok = 0;
                
                    QFutureWatcher<void>* watcher = new QFutureWatcher<void>();
                    connect(watcher, SIGNAL(finished()), this, SLOT(Md5_gen()));
                    //file pak33
                    QString loc33 = "/SM/test.pak";
                    QFuture<QString> future33 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc33);
                    QString pakchunk33 = future33.result();
                    watcher->setFuture(future33);
                    qDebug() << pakchunk33;
                    if (pakchunk33 == "f7002d4419cd235a87746715ba6fb2ea")
                    {
                        qDebug() << "OK";
                        file_ok++;
                        ui->label_8->setText("OK");
                        ui->label_8->setStyleSheet("QLabel { color : green; }");
                    } else if (pakchunk33 == "nofile")
                    {
                        qDebug() << "no file found";
                        ui->label_8->setText("not found");
                        ui->label_8->setStyleSheet("QLabel { color : red; }");
                    } else
                    {
                        qDebug() << "file is diffrent";
                        ui->label_8->setText("wrong");
                        ui->label_8->setStyleSheet("QLabel { color : red; }");
                    }
                
                    //file pak34
                    QString loc34 = "/SM/test2.pak";
                    QFuture<QString> future34 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc34);
                    QString pakchunk34 = future34.result();
                    qDebug() << pakchunk34;
                    watcher->setFuture(future34);
                    if (pakchunk34 == "64c77598586b6c3cb60beed0b0750620")
                    {
                        qDebug() << "OK";
                        file_ok++;
                        ui->label->setText("OK");
                        ui->label->setStyleSheet("QLabel { color : green; }");
                    } else if (pakchunk34 == "nofile")
                    {
                        qDebug() << "no file found";
                        ui->label->setText("not found");
                        ui->label->setStyleSheet("QLabel { color : red; }");
                    } else
                    {
                        qDebug() << "file is diffrent";
                        ui->label->setText("wrong");
                        ui->label->setStyleSheet("QLabel { color : red; }");
                    }
                
                    //file pak35
                    QString loc35 = "/SM/test3.pak";
                    QFuture<QString> future35 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc35);
                    QString pakchunk35 = future35.result();
                    watcher->setFuture(future35);
                    qDebug() << pakchunk35;
                    if (pakchunk35 == "ee53f7a7656a32b5278c460baec46f16")
                    {
                        qDebug() << "OK";
                        file_ok++;
                        ui->label_7->setText("OK");
                        ui->label_7->setStyleSheet("QLabel { color : green; }");
                    } else if (pakchunk35 == "nofile")
                    {
                        qDebug() << "no file found";
                        ui->label_7->setText("not found");
                        ui->label_7->setStyleSheet("QLabel { color : red; }");
                    } else
                    {
                        qDebug() << "file is diffrent";
                        ui->label_7->setText("wrong");
                        ui->label_7->setStyleSheet("QLabel { color : red; }");
                    }
                }
                
                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #20

                Hi @saeid0034,

                see e.g. this example.

                Regards

                Qt has to stay free or it will die.

                S 1 Reply Last reply
                3
                • aha_1980A aha_1980

                  Hi @saeid0034,

                  see e.g. this example.

                  Regards

                  S Offline
                  S Offline
                  saeid0034
                  wrote on last edited by saeid0034
                  #21

                  @aha_1980 thanks, with this example gui run better, but on big file (1.5GB >) gui still freeze for some sec! what i need to do?

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • S saeid0034

                    @aha_1980 thanks, with this example gui run better, but on big file (1.5GB >) gui still freeze for some sec! what i need to do?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #22

                    @saeid0034 said in Program freeze when try to get MD5 of files:

                    what i need to do?

                    fix your code to properly run the task in a separate thread.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    S 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      @saeid0034 said in Program freeze when try to get MD5 of files:

                      what i need to do?

                      fix your code to properly run the task in a separate thread.

                      S Offline
                      S Offline
                      saeid0034
                      wrote on last edited by saeid0034
                      #23

                      @Christian-Ehrlicher my code

                      main.cpp

                      #include "user_def.h"
                      #include "mainwindow2.h"
                      #include...
                      
                      QString Md5_gen(QString const &s)
                      {
                          QString pakchunk_Md5;
                          QCryptographicHash crypto(QCryptographicHash::Md5);
                          QFile pakchunk("D:/Games/TDPA - Man of Medan" + s);
                          if (pakchunk.open(QIODevice::ReadOnly))
                          {
                              while(!pakchunk.atEnd()){
                              crypto.addData(pakchunk.read(8192));
                              }
                          } else
                          {
                              qDebug() << "Can't open file.";
                              pakchunk_Md5 = "nofile";
                              return pakchunk_Md5;
                          }
                          pakchunk_Md5 = crypto.result().toHex();
                          return pakchunk_Md5;
                      }
                      
                      int main(int argc, char *argv[]) {
                        QApplication a(argc, argv);
                      
                        a.setStyle(new DarkStyle);
                      
                        FramelessWindow framelessWindow;
                        framelessWindow.setWindowIcon(a.style()->standardIcon(QStyle::SP_DesktopIcon));
                      
                        MainWindow *mainWindow = new MainWindow;
                      
                        framelessWindow.setContent(mainWindow);
                        framelessWindow.show();
                      
                        return a.exec();
                      }
                      

                      user_def.h

                      #ifndef USER_DEF_H
                      #define USER_DEF_H
                      #include <QString>
                      
                      QString Md5_gen(QString const &s);
                      
                      #endif // USER_DEF_H
                      
                      

                      mainwindow2.h

                      #ifndef MAINWINDOW2_H
                      #define MAINWINDOW2_H
                      
                      #include <QMainWindow>
                      #include <QtConcurrentRun>
                      #include <QFuture>
                      #include <QFutureWatcher>
                      #include <QThread>
                      #include <QThreadPool>
                      #include "user_def.h"
                      
                      namespace Ui {
                      class MainWindow2;
                      }
                      
                      class MainWindow2 : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          explicit MainWindow2(QWidget *parent = nullptr);
                          ~MainWindow2();
                      
                      public slots:
                            void run_thread();
                            void displayFinishedBox();
                      
                      private slots:
                          void on_pushButton_clicked();
                      
                          void on_pushButton_2_clicked();
                      
                          void on_radioButton_2_clicked();
                      
                          void on_radioButton_4_clicked();
                      
                          void on_radioButton_3_clicked();
                      
                          void on_radioButton_clicked();
                      
                      private:
                          Ui::MainWindow2 *ui;
                          QFutureWatcher<QString> *watcher;
                          QFuture<QString> *future;
                      };
                      
                      #endif // MAINWINDOW2_H
                      

                      mainwindow2.cpp

                      #include...
                      
                      MainWindow2::MainWindow2(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow2)
                      {
                          ui->setupUi(this);
                          connect(ui->pushButton, &QPushButton::clicked,
                                     this, &MainWindow2::run_thread);
                      
                      
                          // display a message box when the calculation has finished
                      
                          future = new QFuture<QString>;
                          watcher = new QFutureWatcher<QString>;
                      
                          connect(watcher, SIGNAL(finished()),
                                  this, SLOT(displayFinishedBox()));
                      
                      }
                      
                      MainWindow2::~MainWindow2()
                      {
                          delete ui;
                      }
                      
                      void MainWindow2::run_thread()
                      {
                          int file_ok = 0;
                          //file pak33
                          QString loc33 = "/SM/test1.pak";
                          QFuture<QString> future33 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc33);
                          watcher->setFuture(future33);
                          QString pakchunk33 = future33.result();
                      
                          qDebug() << pakchunk33;
                          if (pakchunk33 == "f7002d4419cd235a87746715ba6fb2ea")
                          {
                              qDebug() << "OK";
                              file_ok++;
                              ui->label_8->setText("OK");
                              ui->label_8->setStyleSheet("QLabel { color : green; }");
                          } else if (pakchunk33 == "nofile")
                          {
                              qDebug() << "no file found";
                              ui->label_8->setText("not found");
                              ui->label_8->setStyleSheet("QLabel { color : red; }");
                          } else
                          {
                              qDebug() << "file is diffrent";
                              ui->label_8->setText("wrong");
                              ui->label_8->setStyleSheet("QLabel { color : red; }");
                          }
                          ui->progressBar->setValue(12);
                      
                          //file pak34
                          QString loc34 = "/SM/test2.pak";
                          QFuture<QString> future34 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc34);
                          watcher->setFuture(future34);
                          QString pakchunk34 = future34.result();
                          qDebug() << pakchunk34;
                      
                          if (pakchunk34 == "64c77598586b6c3cb60beed0b0750620")
                          {
                              qDebug() << "OK";
                              file_ok++;
                              ui->label->setText("OK");
                              ui->label->setStyleSheet("QLabel { color : green; }");
                          } else if (pakchunk34 == "nofile")
                          {
                              qDebug() << "no file found";
                              ui->label->setText("not found");
                              ui->label->setStyleSheet("QLabel { color : red; }");
                          } else
                          {
                              qDebug() << "file is diffrent";
                              ui->label->setText("wrong");
                              ui->label->setStyleSheet("QLabel { color : red; }");
                          }
                          ui->progressBar->setValue(25);
                      
                          //file pak35
                          QString loc35 = "/SM/test3.pak";
                          QFuture<QString> future35 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc35);
                          watcher->setFuture(future35);
                          QString pakchunk35 = future35.result();
                      
                          qDebug() << pakchunk35;
                          if (pakchunk35 == "ee53f7a7656a32b5278c460baec46f16")
                          {
                              qDebug() << "OK";
                              file_ok++;
                              ui->label_7->setText("OK");
                              ui->label_7->setStyleSheet("QLabel { color : green; }");
                          } else if (pakchunk35 == "nofile")
                          {
                              qDebug() << "no file found";
                              ui->label_7->setText("not found");
                              ui->label_7->setStyleSheet("QLabel { color : red; }");
                          } else
                          {
                              qDebug() << "file is diffrent";
                              ui->label_7->setText("wrong");
                              ui->label_7->setStyleSheet("QLabel { color : red; }");
                          }
                          ui->progressBar->setValue(38);
                      
                          /*Some other code*/
                      

                      i dont know what is problem on this code

                      JonBJ 1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #24

                        @saeid0034 said in Program freeze when try to get MD5 of files:

                        i dont know what is problem on this code

                        You don't read the post from others:

                        As @SGaist already said, calling result() when the future is not done yet will block your event loop.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        S 1 Reply Last reply
                        4
                        • Christian EhrlicherC Christian Ehrlicher

                          @saeid0034 said in Program freeze when try to get MD5 of files:

                          i dont know what is problem on this code

                          You don't read the post from others:

                          As @SGaist already said, calling result() when the future is not done yet will block your event loop.

                          S Offline
                          S Offline
                          saeid0034
                          wrote on last edited by
                          #25

                          @Christian-Ehrlicher i follow

                          @aha_1980 said in Program freeze when try to get MD5 of files:

                          see e.g. this example.

                          example, i read QtConcurrent doc and try to use it with watcher but after all i dont have any luck

                          SGaistS 1 Reply Last reply
                          -1
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #26

                            The example shows you exactly how to use it. Post some code of your try when you follow the example.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            2
                            • S saeid0034

                              @Christian-Ehrlicher i follow

                              @aha_1980 said in Program freeze when try to get MD5 of files:

                              see e.g. this example.

                              example, i read QtConcurrent doc and try to use it with watcher but after all i dont have any luck

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #27

                              @saeid0034 said in Program freeze when try to get MD5 of files:

                              @Christian-Ehrlicher i follow

                              @aha_1980 said in Program freeze when try to get MD5 of files:

                              see e.g. this example.

                              example, i read QtConcurrent doc and try to use it with watcher but after all i dont have any luck

                              You just added a watcher to your code, as already written several times: stop using result right after your run calls.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              3
                              • S saeid0034

                                @Christian-Ehrlicher my code

                                main.cpp

                                #include "user_def.h"
                                #include "mainwindow2.h"
                                #include...
                                
                                QString Md5_gen(QString const &s)
                                {
                                    QString pakchunk_Md5;
                                    QCryptographicHash crypto(QCryptographicHash::Md5);
                                    QFile pakchunk("D:/Games/TDPA - Man of Medan" + s);
                                    if (pakchunk.open(QIODevice::ReadOnly))
                                    {
                                        while(!pakchunk.atEnd()){
                                        crypto.addData(pakchunk.read(8192));
                                        }
                                    } else
                                    {
                                        qDebug() << "Can't open file.";
                                        pakchunk_Md5 = "nofile";
                                        return pakchunk_Md5;
                                    }
                                    pakchunk_Md5 = crypto.result().toHex();
                                    return pakchunk_Md5;
                                }
                                
                                int main(int argc, char *argv[]) {
                                  QApplication a(argc, argv);
                                
                                  a.setStyle(new DarkStyle);
                                
                                  FramelessWindow framelessWindow;
                                  framelessWindow.setWindowIcon(a.style()->standardIcon(QStyle::SP_DesktopIcon));
                                
                                  MainWindow *mainWindow = new MainWindow;
                                
                                  framelessWindow.setContent(mainWindow);
                                  framelessWindow.show();
                                
                                  return a.exec();
                                }
                                

                                user_def.h

                                #ifndef USER_DEF_H
                                #define USER_DEF_H
                                #include <QString>
                                
                                QString Md5_gen(QString const &s);
                                
                                #endif // USER_DEF_H
                                
                                

                                mainwindow2.h

                                #ifndef MAINWINDOW2_H
                                #define MAINWINDOW2_H
                                
                                #include <QMainWindow>
                                #include <QtConcurrentRun>
                                #include <QFuture>
                                #include <QFutureWatcher>
                                #include <QThread>
                                #include <QThreadPool>
                                #include "user_def.h"
                                
                                namespace Ui {
                                class MainWindow2;
                                }
                                
                                class MainWindow2 : public QMainWindow
                                {
                                    Q_OBJECT
                                
                                public:
                                    explicit MainWindow2(QWidget *parent = nullptr);
                                    ~MainWindow2();
                                
                                public slots:
                                      void run_thread();
                                      void displayFinishedBox();
                                
                                private slots:
                                    void on_pushButton_clicked();
                                
                                    void on_pushButton_2_clicked();
                                
                                    void on_radioButton_2_clicked();
                                
                                    void on_radioButton_4_clicked();
                                
                                    void on_radioButton_3_clicked();
                                
                                    void on_radioButton_clicked();
                                
                                private:
                                    Ui::MainWindow2 *ui;
                                    QFutureWatcher<QString> *watcher;
                                    QFuture<QString> *future;
                                };
                                
                                #endif // MAINWINDOW2_H
                                

                                mainwindow2.cpp

                                #include...
                                
                                MainWindow2::MainWindow2(QWidget *parent) :
                                    QMainWindow(parent),
                                    ui(new Ui::MainWindow2)
                                {
                                    ui->setupUi(this);
                                    connect(ui->pushButton, &QPushButton::clicked,
                                               this, &MainWindow2::run_thread);
                                
                                
                                    // display a message box when the calculation has finished
                                
                                    future = new QFuture<QString>;
                                    watcher = new QFutureWatcher<QString>;
                                
                                    connect(watcher, SIGNAL(finished()),
                                            this, SLOT(displayFinishedBox()));
                                
                                }
                                
                                MainWindow2::~MainWindow2()
                                {
                                    delete ui;
                                }
                                
                                void MainWindow2::run_thread()
                                {
                                    int file_ok = 0;
                                    //file pak33
                                    QString loc33 = "/SM/test1.pak";
                                    QFuture<QString> future33 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc33);
                                    watcher->setFuture(future33);
                                    QString pakchunk33 = future33.result();
                                
                                    qDebug() << pakchunk33;
                                    if (pakchunk33 == "f7002d4419cd235a87746715ba6fb2ea")
                                    {
                                        qDebug() << "OK";
                                        file_ok++;
                                        ui->label_8->setText("OK");
                                        ui->label_8->setStyleSheet("QLabel { color : green; }");
                                    } else if (pakchunk33 == "nofile")
                                    {
                                        qDebug() << "no file found";
                                        ui->label_8->setText("not found");
                                        ui->label_8->setStyleSheet("QLabel { color : red; }");
                                    } else
                                    {
                                        qDebug() << "file is diffrent";
                                        ui->label_8->setText("wrong");
                                        ui->label_8->setStyleSheet("QLabel { color : red; }");
                                    }
                                    ui->progressBar->setValue(12);
                                
                                    //file pak34
                                    QString loc34 = "/SM/test2.pak";
                                    QFuture<QString> future34 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc34);
                                    watcher->setFuture(future34);
                                    QString pakchunk34 = future34.result();
                                    qDebug() << pakchunk34;
                                
                                    if (pakchunk34 == "64c77598586b6c3cb60beed0b0750620")
                                    {
                                        qDebug() << "OK";
                                        file_ok++;
                                        ui->label->setText("OK");
                                        ui->label->setStyleSheet("QLabel { color : green; }");
                                    } else if (pakchunk34 == "nofile")
                                    {
                                        qDebug() << "no file found";
                                        ui->label->setText("not found");
                                        ui->label->setStyleSheet("QLabel { color : red; }");
                                    } else
                                    {
                                        qDebug() << "file is diffrent";
                                        ui->label->setText("wrong");
                                        ui->label->setStyleSheet("QLabel { color : red; }");
                                    }
                                    ui->progressBar->setValue(25);
                                
                                    //file pak35
                                    QString loc35 = "/SM/test3.pak";
                                    QFuture<QString> future35 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc35);
                                    watcher->setFuture(future35);
                                    QString pakchunk35 = future35.result();
                                
                                    qDebug() << pakchunk35;
                                    if (pakchunk35 == "ee53f7a7656a32b5278c460baec46f16")
                                    {
                                        qDebug() << "OK";
                                        file_ok++;
                                        ui->label_7->setText("OK");
                                        ui->label_7->setStyleSheet("QLabel { color : green; }");
                                    } else if (pakchunk35 == "nofile")
                                    {
                                        qDebug() << "no file found";
                                        ui->label_7->setText("not found");
                                        ui->label_7->setStyleSheet("QLabel { color : red; }");
                                    } else
                                    {
                                        qDebug() << "file is diffrent";
                                        ui->label_7->setText("wrong");
                                        ui->label_7->setStyleSheet("QLabel { color : red; }");
                                    }
                                    ui->progressBar->setValue(38);
                                
                                    /*Some other code*/
                                

                                i dont know what is problem on this code

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #28

                                @saeid0034 said in Program freeze when try to get MD5 of files:

                                QFuture<QString> future33 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc33);
                                watcher->setFuture(future33);
                                QString pakchunk33 = future33.result();
                                

                                I am not saying anything that @SGaist & @Christian-Ehrlicher have not said already, but perhaps this will clarify.

                                Examine these three adjacent lines in your code:

                                1. First line sets off the computation to run concurrently in another thread. It ha started now, but (probably) not finished.
                                2. Second line places a "watcher" on the thread/result, to notify when it does finish. Thread is still running.
                                3. Third line wants the result from the thread. so it stops/blocks on that line until the thread finishes and the result is available. Code does not proceed from that line now, till the thread is done.

                                It's line #3 that you cannot afford to put where it is currently. The code which needs to read the result of the thread must not be executed till the thread is ready to deliver it, which will come in a slot on the watcher. Your code which works with the computed result belongs there. This is how you should rewrite your code.

                                Or, if you have let it start and done some stuff of your own here, and then you get "fed up" of waiting and have nothing else to do, you might at that point call future33.result(). But be aware that if you call it anywhere while the thread has not yet finished you will block on the call till it does. And that will stop your calling thread from doing anything else till it arrives, including servicing the main event loop which is what keeps your Qt UI responsive.

                                S 1 Reply Last reply
                                2
                                • JonBJ JonB

                                  @saeid0034 said in Program freeze when try to get MD5 of files:

                                  QFuture<QString> future33 = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc33);
                                  watcher->setFuture(future33);
                                  QString pakchunk33 = future33.result();
                                  

                                  I am not saying anything that @SGaist & @Christian-Ehrlicher have not said already, but perhaps this will clarify.

                                  Examine these three adjacent lines in your code:

                                  1. First line sets off the computation to run concurrently in another thread. It ha started now, but (probably) not finished.
                                  2. Second line places a "watcher" on the thread/result, to notify when it does finish. Thread is still running.
                                  3. Third line wants the result from the thread. so it stops/blocks on that line until the thread finishes and the result is available. Code does not proceed from that line now, till the thread is done.

                                  It's line #3 that you cannot afford to put where it is currently. The code which needs to read the result of the thread must not be executed till the thread is ready to deliver it, which will come in a slot on the watcher. Your code which works with the computed result belongs there. This is how you should rewrite your code.

                                  Or, if you have let it start and done some stuff of your own here, and then you get "fed up" of waiting and have nothing else to do, you might at that point call future33.result(). But be aware that if you call it anywhere while the thread has not yet finished you will block on the call till it does. And that will stop your calling thread from doing anything else till it arrives, including servicing the main event loop which is what keeps your Qt UI responsive.

                                  S Offline
                                  S Offline
                                  saeid0034
                                  wrote on last edited by saeid0034
                                  #29

                                  @JonB thanks for detailed information
                                  i get it know, i need to wait for watcher to notify thread finished .

                                  so i need first

                                  connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow2::run_thread);
                                  

                                  to start process after that wait until watcher Triger finished signal

                                  connect(watcher, SIGNAL(finished()), this, SLOT(..something..));
                                  

                                  and also i need Create a watcher per future for get finished signal.
                                  right?

                                  but one other thing, how can i get return of MF5_Gen from run_thread to check it?

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #30

                                    Please read the class documentation. There are methods to retrieve the information.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    S 1 Reply Last reply
                                    2
                                    • SGaistS SGaist

                                      Please read the class documentation. There are methods to retrieve the information.

                                      S Offline
                                      S Offline
                                      saeid0034
                                      wrote on last edited by saeid0034
                                      #31

                                      @SGaist thanks my code is it for know
                                      it working good without freeze gui
                                      in term of coding this code is standard?

                                      mainwindow2.cpp

                                      MainWindow2::MainWindow2(QWidget *parent) :
                                          QMainWindow(parent),
                                          ui(new Ui::MainWindow2)
                                      {
                                          ui->setupUi(this);
                                          connect(ui->pushButton_3, &QPushButton::clicked, this, &MainWindow2::MD5_thread_1);
                                      
                                          future = new QFuture<QString>;
                                          watcher1 = new QFutureWatcher<QString>;
                                          watcher2 = new QFutureWatcher<QString>;
                                          watcher3 = new QFutureWatcher<QString>;
                                      
                                          connect(watcher1, SIGNAL(finished()), this, SLOT(MD5_thread_2()));
                                          connect(watcher2, SIGNAL(finished()), this, SLOT(MD5_thread_3()));
                                          connect(watcher3, SIGNAL(finished()), this, SLOT(core_install()));
                                          //some other code
                                      }
                                      
                                      void MainWindow2::MD5_thread_1()
                                      {
                                          ui->pushButton->setEnabled(false);
                                          ui->pushButton->setText("procces started");
                                          ui->pushButton->setStyleSheet("QPushButton { color : white; background-color: rgb(73, 80, 93); }");
                                          ui->label->setText("checking");
                                          ui->label_2->setText("checking");
                                          ui->label_3->setText("checking");
                                          ui->label_4->setText("checking");
                                          ui->label_5->setText("checking");
                                          ui->label_6->setText("checking");
                                          ui->label_7->setText("checking");
                                          ui->label_8->setText("checking");
                                          ui->label_14->setText("waiting for end of check");
                                          ui->progressBar->setRange(0, 100);
                                          ui->progressBar_2->setRange(0, 100);
                                          ui->progressBar->setValue(0);
                                          ui->progressBar_2->setValue(0);
                                      
                                          //file pak33
                                          QString loc33 = "/SMG0/editor.pak";
                                          *future= QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc33);
                                          watcher1->setFuture(*future);
                                      }
                                      
                                      void MainWindow2::MD5_thread_2()
                                      {
                                          QString pakchunk33 = future->result();
                                      
                                          qDebug() << pakchunk33;
                                          if (pakchunk33 == "f7002d4419cd235a87746715ba6fb2ea")
                                          {
                                              qDebug() << "OK";
                                              file_ok++;
                                              ui->label_8->setText("OK");
                                              ui->label_8->setStyleSheet("QLabel { color : green; }");
                                          } else if (pakchunk33 == "nofile")
                                          {
                                              qDebug() << "no file found";
                                              ui->label_8->setText("not found");
                                              ui->label_8->setStyleSheet("QLabel { color : red; }");
                                          } else
                                          {
                                              qDebug() << "file is diffrent";
                                              ui->label_8->setText("wrong");
                                              ui->label_8->setStyleSheet("QLabel { color : red; }");
                                          }
                                          ui->progressBar->setValue(12);
                                      
                                          //file pak34
                                          QString loc34 = "/SMG0/2Editor.pak";
                                          *future = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc34);
                                          watcher2->setFuture(*future);
                                      }
                                      
                                      void MainWindow2::MD5_thread_3()
                                      {
                                          QString pakchunk34 = future->result();
                                          qDebug() << pakchunk34;
                                      
                                          if (pakchunk34 == "64c77598586b6c3cb60beed0b0750620")
                                          {
                                              qDebug() << "OK";
                                              file_ok++;
                                              ui->label->setText("OK");
                                              ui->label->setStyleSheet("QLabel { color : green; }");
                                          } else if (pakchunk34 == "nofile")
                                          {
                                              qDebug() << "no file found";
                                              ui->label->setText("not found");
                                              ui->label->setStyleSheet("QLabel { color : red; }");
                                          } else
                                          {
                                              qDebug() << "file is diffrent";
                                              ui->label->setText("wrong");
                                              ui->label->setStyleSheet("QLabel { color : red; }");
                                          }
                                          ui->progressBar->setValue(25);
                                      
                                          //file pak35
                                          QString loc35 = "/SMG0/3Editor.pak";
                                          *future = QtConcurrent::run(QThreadPool::globalInstance(), Md5_gen, loc35);
                                          watcher3->setFuture(*future);
                                      }
                                      
                                      
                                      void MainWindow2::core_install
                                      {
                                      	QString pakchunk35 = future->result();
                                      
                                          qDebug() << pakchunk40;
                                          if (pakchunk40 == "49e0440340044f424caeb82bade1301f")
                                          {
                                              qDebug() << "OK";
                                              file_ok++;
                                              ui->label_2->setText("OK");
                                              ui->label_2->setStyleSheet("QLabel { color : green; }");
                                          } else if (pakchunk40 == "nofile")
                                          {
                                              qDebug() << "no file found";
                                              ui->label_2->setText("not found");
                                              ui->label_2->setStyleSheet("QLabel { color : red; }");
                                          } else
                                          {
                                              qDebug() << "file is diffrent";
                                              ui->label_2->setText("wrong");
                                              ui->label_2->setStyleSheet("QLabel { color : red; }");
                                          }
                                          ui->progressBar->setValue(100);
                                      
                                          //check if game is okey or not
                                          if (file_ok == 8)
                                          {
                                              ui->label_14->setText("O");
                                              ui->label_14->setStyleSheet("QLabel { color : green; }");
                                          } else
                                          {
                                              ui->label_14->setText("X");
                                              ui->label_14->setStyleSheet("QLabel { color : red; }");
                                          }
                                      }
                                      

                                      mainwindow2.h

                                      #ifndef MAINWINDOW2_H
                                      #define MAINWINDOW2_H
                                      
                                      #include <QMainWindow>
                                      #include <QtConcurrentRun>
                                      #include <QFuture>
                                      #include <QFutureWatcher>
                                      #include <QThread>
                                      #include <QThreadPool>
                                      #include "user_def.h"
                                      
                                      namespace Ui {
                                      class MainWindow2;
                                      }
                                      
                                      class MainWindow2 : public QMainWindow
                                      {
                                          Q_OBJECT
                                      
                                      public:
                                          explicit MainWindow2(QWidget *parent = nullptr);
                                          ~MainWindow2();
                                      
                                      public slots:
                                            void MD5_thread_1();
                                            void MD5_thread_2();
                                            void MD5_thread_3();
                                      	  void core_install();
                                      
                                      private slots:
                                          void on_pushButton_clicked();
                                      
                                          void on_pushButton_2_clicked();
                                      
                                          void on_radioButton_2_clicked();
                                      
                                          void on_radioButton_4_clicked();
                                      
                                          void on_radioButton_3_clicked();
                                      
                                          void on_radioButton_clicked();
                                      
                                      private:
                                          Ui::MainWindow2 *ui;
                                          QFutureWatcher<QString> *watcher1;
                                          QFutureWatcher<QString> *watcher2;
                                          QFutureWatcher<QString> *watcher3;
                                          QFuture<QString> *future;
                                          int file_ok = 0;
                                      };
                                      
                                      #endif // MAINWINDOW2_H
                                      
                                      
                                      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