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. loop for not waiting the end of process
Forum Updated to NodeBB v4.3 + New Features

loop for not waiting the end of process

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 4 Posters 4.9k Views 4 Watching
  • 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.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by
    #1

    Hello,

    trying to convert some files with imagemagick, I did that:

    QDir directory("//DGS1109N007/Echanges_SAA/Affichage/Messages");
        QStringList images = directory.entryList(QStringList(),QDir::Files);
    
        for(int i=0;i<images.count();i++) {
        //do whatever you need to do
        QString filename=images[i];
        qDebug()<<"filename"<<filename;
    
        program=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick ")+ "convert -density 300 -trim "+ QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/"+filename)+"\" -quality 100 "+QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");
        myProcess.start(program);
        myProcess.waitForFinished(10000);
       
        }
    

    the problem is the qdebug print all files but the process don't have time to restart between 2 files, using waitForFinished don't have any effect..

    Any idea?

    thanks a lot

    F sierdzioS 2 Replies Last reply
    0
    • F filipdns

      Hello,

      trying to convert some files with imagemagick, I did that:

      QDir directory("//DGS1109N007/Echanges_SAA/Affichage/Messages");
          QStringList images = directory.entryList(QStringList(),QDir::Files);
      
          for(int i=0;i<images.count();i++) {
          //do whatever you need to do
          QString filename=images[i];
          qDebug()<<"filename"<<filename;
      
          program=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick ")+ "convert -density 300 -trim "+ QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/"+filename)+"\" -quality 100 "+QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");
          myProcess.start(program);
          myProcess.waitForFinished(10000);
         
          }
      

      the problem is the qdebug print all files but the process don't have time to restart between 2 files, using waitForFinished don't have any effect..

      Any idea?

      thanks a lot

      F Offline
      F Offline
      filipdns
      wrote on last edited by
      #2

      @filipdns I will try QProcess::ProcessState to make stand by before pass to other files, I will give feedback tomorrow morning, if you have other idea, tell me.

      thanks guys for your help

      1 Reply Last reply
      0
      • F filipdns

        Hello,

        trying to convert some files with imagemagick, I did that:

        QDir directory("//DGS1109N007/Echanges_SAA/Affichage/Messages");
            QStringList images = directory.entryList(QStringList(),QDir::Files);
        
            for(int i=0;i<images.count();i++) {
            //do whatever you need to do
            QString filename=images[i];
            qDebug()<<"filename"<<filename;
        
            program=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick ")+ "convert -density 300 -trim "+ QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/"+filename)+"\" -quality 100 "+QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");
            myProcess.start(program);
            myProcess.waitForFinished(10000);
           
            }
        

        the problem is the qdebug print all files but the process don't have time to restart between 2 files, using waitForFinished don't have any effect..

        Any idea?

        thanks a lot

        sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        @filipdns said in loop for not waiting the end of process:

        the process don't have time to restart between 2 files, using waitForFinished don't have any effect..

        Declare QProcess inside the loop, don't use a global instance. This is not what QProcess was designed to do.

        Are you sure the process actually starts? Your paths look weird (what's that double slash at their beginning? Are you trying to run something from a network storage? That is likely to fail).

        Please check return values and error outputs to see if QProcess actually starts.

        (Z(:^

        F 1 Reply Last reply
        2
        • sierdzioS sierdzio

          @filipdns said in loop for not waiting the end of process:

          the process don't have time to restart between 2 files, using waitForFinished don't have any effect..

          Declare QProcess inside the loop, don't use a global instance. This is not what QProcess was designed to do.

          Are you sure the process actually starts? Your paths look weird (what's that double slash at their beginning? Are you trying to run something from a network storage? That is likely to fail).

          Please check return values and error outputs to see if QProcess actually starts.

          F Offline
          F Offline
          filipdns
          wrote on last edited by filipdns
          #4

          @sierdzio Hi,

          Yes the process is working, I seen the ouput file created without any problem and I don't have any error and yes the double slash is to point network exe but it's not that the problem.

          I'm may be wrong but it's doing like in loop, the process is launch but the loop don't stop until the process is funished, the loop return immediately after launch it.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            If the process works, and you see the output file being created, then I'd say there is no problem at all... it works. If you mean that next loop iteration overwrites your process or something, then do as I suggested and create new QProcess instance inside the loop.

            PLease note that waitForFinished will wait at most the specified amount of time. If process ends sooner, it will stop waiting. So if your process finishes extremely fast, you may not even notice any delay.

            BTW. You probably should call waitForStarted() before calling waitForFinished().

            (Z(:^

            F 1 Reply Last reply
            3
            • sierdzioS sierdzio

              If the process works, and you see the output file being created, then I'd say there is no problem at all... it works. If you mean that next loop iteration overwrites your process or something, then do as I suggested and create new QProcess instance inside the loop.

              PLease note that waitForFinished will wait at most the specified amount of time. If process ends sooner, it will stop waiting. So if your process finishes extremely fast, you may not even notice any delay.

              BTW. You probably should call waitForStarted() before calling waitForFinished().

              F Offline
              F Offline
              filipdns
              wrote on last edited by
              #6

              @sierdzio it almost work, the problem is only one output file is created, but it suppose to have many one (images.count() return 5)

              using .bat with

              for %%f in ("\\DGS1109N007\Echanges_SAA\Affichage\Messages\*.*") do ( \\DGS1109N007\Echanges_SAA\Affichage\Display\imagmagick\magick convert -density 300 -trim "\\DGS1109N007\Echanges_SAA\Affichage\Messages\*.*" -quality 100 "\\DGS1109N007\Echanges_SAA\Affichage\Display\Messages\output.jpg" )
              

              in the ouput folder I got all 5 files named output.jpg, output-0.jpg, output-1.jpg etc...

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

                Hi,

                You should try to properly build the argument list as a QStringList as show in QProcess' doc.

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

                F 1 Reply Last reply
                2
                • F filipdns

                  @sierdzio it almost work, the problem is only one output file is created, but it suppose to have many one (images.count() return 5)

                  using .bat with

                  for %%f in ("\\DGS1109N007\Echanges_SAA\Affichage\Messages\*.*") do ( \\DGS1109N007\Echanges_SAA\Affichage\Display\imagmagick\magick convert -density 300 -trim "\\DGS1109N007\Echanges_SAA\Affichage\Messages\*.*" -quality 100 "\\DGS1109N007\Echanges_SAA\Affichage\Display\Messages\output.jpg" )
                  

                  in the ouput folder I got all 5 files named output.jpg, output-0.jpg, output-1.jpg etc...

                  F Offline
                  F Offline
                  filipdns
                  wrote on last edited by
                  #8

                  @filipdns I will try to change the process to . in place of file name, I will see if it work but I doubt because in the .bat, I have to have for %%f in to make it convert all files, that why I pass trough loop for or foreach but without success

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    You should try to properly build the argument list as a QStringList as show in QProcess' doc.

                    F Offline
                    F Offline
                    filipdns
                    wrote on last edited by
                    #9

                    @SGaist Hi SGaist, building with argument was my first attempt, but I have exactly the same result

                    F 1 Reply Last reply
                    0
                    • F filipdns

                      @SGaist Hi SGaist, building with argument was my first attempt, but I have exactly the same result

                      F Offline
                      F Offline
                      filipdns
                      wrote on last edited by
                      #10

                      @filipdns

                      here how was my first code

                      program=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick.exe ")
                      QStringList arguments;
                      arguments<< "convert -density 300 -trim "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/")+filename<<" -quality 100 "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");
                      
                      myProcess.start(program,arguments);
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • F filipdns

                        @filipdns

                        here how was my first code

                        program=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick.exe ")
                        QStringList arguments;
                        arguments<< "convert -density 300 -trim "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/")+filename<<" -quality 100 "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");
                        
                        myProcess.start(program,arguments);
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @filipdns said in loop for not waiting the end of process:

                        arguments<< "convert -density 300 -trim "<<QDir::toNativeSeparators(""//DGS1109N007/Echanges_SAA/Affichage/Messages/")+filename<<" -quality 100 "<<QDir::toNativeSeparators(""//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");

                        This is wrong. EVERY parameter needs to be an element in parameter list. Like:

                        arguments<< "convert" << "-density" << "300" << "-trim "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/")+filename << "-quality" << "100 "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        F 1 Reply Last reply
                        6
                        • jsulmJ jsulm

                          @filipdns said in loop for not waiting the end of process:

                          arguments<< "convert -density 300 -trim "<<QDir::toNativeSeparators(""//DGS1109N007/Echanges_SAA/Affichage/Messages/")+filename<<" -quality 100 "<<QDir::toNativeSeparators(""//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");

                          This is wrong. EVERY parameter needs to be an element in parameter list. Like:

                          arguments<< "convert" << "-density" << "300" << "-trim "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/")+filename << "-quality" << "100 "<<QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/output.jpg");
                          
                          F Offline
                          F Offline
                          filipdns
                          wrote on last edited by
                          #12

                          @jsulm Hello, I try that but it's not working.

                          I solve my issue with:

                                  program=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick ")+ "convert -density 300 -trim "+ QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/"+filename)+"\" -quality 100 "+QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/"+outfilename+".jpg");
                          
                                  if(myProcess.state()!= QProcess::Running)
                                  {
                                      myProcess.start(program);
                                      myProcess.waitForFinished(5000);
                                      i=i+1;
                                  }
                          

                          thank you very much for you help!!

                          sierdzioS 1 Reply Last reply
                          1
                          • F filipdns

                            @jsulm Hello, I try that but it's not working.

                            I solve my issue with:

                                    program=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick ")+ "convert -density 300 -trim "+ QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/"+filename)+"\" -quality 100 "+QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/"+outfilename+".jpg");
                            
                                    if(myProcess.state()!= QProcess::Running)
                                    {
                                        myProcess.start(program);
                                        myProcess.waitForFinished(5000);
                                        i=i+1;
                                    }
                            

                            thank you very much for you help!!

                            sierdzioS Offline
                            sierdzioS Offline
                            sierdzio
                            Moderators
                            wrote on last edited by
                            #13

                            @filipdns said in loop for not waiting the end of process:

                            i=i+1;

                            That means you are skipping one image for every loop iteration.

                            if(myProcess.state()!= QProcess::Running)

                            And that means you may skip many more if process is still running. You could have avoided that if you created local QProcess inside the loop...

                            (Z(:^

                            F 1 Reply Last reply
                            3
                            • sierdzioS sierdzio

                              @filipdns said in loop for not waiting the end of process:

                              i=i+1;

                              That means you are skipping one image for every loop iteration.

                              if(myProcess.state()!= QProcess::Running)

                              And that means you may skip many more if process is still running. You could have avoided that if you created local QProcess inside the loop...

                              F Offline
                              F Offline
                              filipdns
                              wrote on last edited by
                              #14

                              @sierdzio hi, no it's not skipping any image but what do you mean with process inside the loop?

                              sierdzioS 1 Reply Last reply
                              0
                              • F filipdns

                                @sierdzio hi, no it's not skipping any image but what do you mean with process inside the loop?

                                sierdzioS Offline
                                sierdzioS Offline
                                sierdzio
                                Moderators
                                wrote on last edited by
                                #15

                                @filipdns said in loop for not waiting the end of process:

                                @sierdzio hi, no it's not skipping any image

                                OK then you probably have removed i++ from first line of your loop? Then that has another side effect - until your process exits you are forcing a tight, endless loop. That will eat your CPU, don't do it.

                                Also, if QProcess gets stuck for any reason, that loop will never exit.

                                but what do you mean with process inside the loop?

                                for (whatever) {
                                  QProcess process;
                                  process.start(program);
                                  process.waitForStarted(5000);
                                  if(process.waitForFinished(5000) == false)
                                    qInfo() << "Process had some trouble" << process.errorString() << program;
                                }
                                

                                (Z(:^

                                F 1 Reply Last reply
                                3
                                • sierdzioS sierdzio

                                  @filipdns said in loop for not waiting the end of process:

                                  @sierdzio hi, no it's not skipping any image

                                  OK then you probably have removed i++ from first line of your loop? Then that has another side effect - until your process exits you are forcing a tight, endless loop. That will eat your CPU, don't do it.

                                  Also, if QProcess gets stuck for any reason, that loop will never exit.

                                  but what do you mean with process inside the loop?

                                  for (whatever) {
                                    QProcess process;
                                    process.start(program);
                                    process.waitForStarted(5000);
                                    if(process.waitForFinished(5000) == false)
                                      qInfo() << "Process had some trouble" << process.errorString() << program;
                                  }
                                  
                                  F Offline
                                  F Offline
                                  filipdns
                                  wrote on last edited by filipdns
                                  #16

                                  @sierdzio Yes I removed the I++ from first line. I didn't know that will have effect on my cup, I will change that and see if your advise can do the job, thanks a lot

                                  sierdzioS 1 Reply Last reply
                                  0
                                  • F filipdns

                                    @sierdzio Yes I removed the I++ from first line. I didn't know that will have effect on my cup, I will change that and see if your advise can do the job, thanks a lot

                                    sierdzioS Offline
                                    sierdzioS Offline
                                    sierdzio
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    @filipdns said in loop for not waiting the end of process:

                                    @sierdzio Yes I removed the I++ from first line. I didn't know that will have effect on my cup, I will change that and see if your advise can do the job, thanks a lot

                                    You can check the endless loop easily. Print a message at the beginning of the loop:

                                    for (...) {
                                      qDebug() << "Loop block begins";
                                    }
                                    

                                    If you see lots and lots of these lines (more than the number of images you have) then it's looping tightly as I mentioned in my last reply.

                                    (Z(:^

                                    F 1 Reply Last reply
                                    0
                                    • sierdzioS sierdzio

                                      @filipdns said in loop for not waiting the end of process:

                                      @sierdzio Yes I removed the I++ from first line. I didn't know that will have effect on my cup, I will change that and see if your advise can do the job, thanks a lot

                                      You can check the endless loop easily. Print a message at the beginning of the loop:

                                      for (...) {
                                        qDebug() << "Loop block begins";
                                      }
                                      

                                      If you see lots and lots of these lines (more than the number of images you have) then it's looping tightly as I mentioned in my last reply.

                                      F Offline
                                      F Offline
                                      filipdns
                                      wrote on last edited by filipdns
                                      #18

                                      @sierdzio I try to move the i=i+1 to the i++ in the line for(int i=0;i<images.count();i++) and it's working fine only if I keep the waitForFinished(5000) otherwise, I have infinit loop

                                          for(int i=0;i<images.count();i++) {
                                              //do whatever you need to do
                                              QString filename=images[i];
                                              QString outfilename=filename.split(".")[0];
                                              program2=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick ")+ "convert -density 300 -trim "+ QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/"+filename)+"\" -quality 100 "+QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/"+outfilename+".jpg");
                                      
                                              if(myProcess2.state()!= QProcess::Running)
                                              {
                                                  myProcess2.start(program2);
                                                  myProcess2.waitForFinished(5000);
                                                  //i=i+1;
                                              }
                                      
                                          }
                                      
                                      jsulmJ 1 Reply Last reply
                                      0
                                      • F filipdns

                                        @sierdzio I try to move the i=i+1 to the i++ in the line for(int i=0;i<images.count();i++) and it's working fine only if I keep the waitForFinished(5000) otherwise, I have infinit loop

                                            for(int i=0;i<images.count();i++) {
                                                //do whatever you need to do
                                                QString filename=images[i];
                                                QString outfilename=filename.split(".")[0];
                                                program2=QDir::toNativeSeparators("//DGS1109N007/Echanges_SAA/Affichage/Display/imagmagick/magick ")+ "convert -density 300 -trim "+ QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Messages/"+filename)+"\" -quality 100 "+QDir::toNativeSeparators("\"//DGS1109N007/Echanges_SAA/Affichage/Display/Messages/"+outfilename+".jpg");
                                        
                                                if(myProcess2.state()!= QProcess::Running)
                                                {
                                                    myProcess2.start(program2);
                                                    myProcess2.waitForFinished(5000);
                                                    //i=i+1;
                                                }
                                        
                                            }
                                        
                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @filipdns You still don't use QProcess inside the loop as suggested by @sierdzio

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        F 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @filipdns You still don't use QProcess inside the loop as suggested by @sierdzio

                                          F Offline
                                          F Offline
                                          filipdns
                                          wrote on last edited by
                                          #20

                                          @jsulm Hello, I seen this advise but I didn't understand how I have to do else than put the process in the loop

                                          jsulmJ F sierdzioS 3 Replies 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