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. QProcess detection running or finished state - hints needed
Forum Updated to NodeBB v4.3 + New Features

QProcess detection running or finished state - hints needed

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 2.9k Views 1 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.
  • MasterBLBM Offline
    MasterBLBM Offline
    MasterBLB
    wrote on last edited by
    #1

    Qt mates, I need your help

    The issue is as follow - I have a QProcess started, and I need either to detect when it has been finished, or if it is still running.
    1st attempt was:

                qint64 pid = 0;
                QProcess::startDetached("cmd", args, qApp->applicationDirPath() + "/Stuff", &pid);
    

    sadly, the returned pid is incorrect (Qt bug probably) compared to what Windows Task Manager shows, so I couldn't use https://stackoverflow.com/questions/13633797/how-to-check-if-a-program-is-running-by-its-name-with-qt-c/13635127#13635127

    Well then, another attempt:

                QProcess *process = new QProcess(parent);
                bool b = QObject::connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), parent, SLOT(finished(int, QProcess::ExitStatus)));
                qDebug() << "połączenie sygnału finished: " << b;
                b = QObject::connect(process, SIGNAL(errorOccurred(QProcess::ProcessError)), parent, SLOT(errorOccurred(QProcess::ProcessError)));
                qDebug() << "połączenie sygnału error: " << b;
                b = QObject::connect(process, SIGNAL(started()), parent, SLOT(processStarted()));
                qDebug() << "połączenie sygnału started: " << b;
    
                process->setWorkingDirectory(qApp->applicationDirPath() + "/Stuff");
                process->setArguments(args);
                process->setProgram("cmd");            
                process->start();
    

    NOPE - finished() signal is not emitted. Hmmm, so maybe this approach:

    //all the stuff as above, except start:
                int returnCode = process->execute("cmd", args);
    

    and guess what, NOPE. Such call does not respect setWorkingDirectory(); a pity as execute() is the closest to my needs.

    Tell me mates, what else I could use?

    OS Windows 7, Qt 5.11.1

    KroMignonK 1 Reply Last reply
    0
    • MasterBLBM MasterBLB

      Qt mates, I need your help

      The issue is as follow - I have a QProcess started, and I need either to detect when it has been finished, or if it is still running.
      1st attempt was:

                  qint64 pid = 0;
                  QProcess::startDetached("cmd", args, qApp->applicationDirPath() + "/Stuff", &pid);
      

      sadly, the returned pid is incorrect (Qt bug probably) compared to what Windows Task Manager shows, so I couldn't use https://stackoverflow.com/questions/13633797/how-to-check-if-a-program-is-running-by-its-name-with-qt-c/13635127#13635127

      Well then, another attempt:

                  QProcess *process = new QProcess(parent);
                  bool b = QObject::connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), parent, SLOT(finished(int, QProcess::ExitStatus)));
                  qDebug() << "połączenie sygnału finished: " << b;
                  b = QObject::connect(process, SIGNAL(errorOccurred(QProcess::ProcessError)), parent, SLOT(errorOccurred(QProcess::ProcessError)));
                  qDebug() << "połączenie sygnału error: " << b;
                  b = QObject::connect(process, SIGNAL(started()), parent, SLOT(processStarted()));
                  qDebug() << "połączenie sygnału started: " << b;
      
                  process->setWorkingDirectory(qApp->applicationDirPath() + "/Stuff");
                  process->setArguments(args);
                  process->setProgram("cmd");            
                  process->start();
      

      NOPE - finished() signal is not emitted. Hmmm, so maybe this approach:

      //all the stuff as above, except start:
                  int returnCode = process->execute("cmd", args);
      

      and guess what, NOPE. Such call does not respect setWorkingDirectory(); a pity as execute() is the closest to my needs.

      Tell me mates, what else I could use?

      OS Windows 7, Qt 5.11.1

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

      @MasterBLB You never get finished() because the process is always running!

      Do you have add "/c" as argument for the cmd process to ensure cmd will terminate after running command?

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

      1 Reply Last reply
      1
      • MasterBLBM Offline
        MasterBLBM Offline
        MasterBLB
        wrote on last edited by
        #3

        Sure thing:

        args << "/C" << "start" << "cmd_ddl.bat";
        
        aha_1980A KroMignonK 2 Replies Last reply
        0
        • MasterBLBM MasterBLB

          Sure thing:

          args << "/C" << "start" << "cmd_ddl.bat";
          
          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MasterBLB said in QProcess detection running or finished state - hints needed:

          args << "/C" << "start" << "cmd_ddl.bat";

          Why "start" ? May it be that this creates a second cmd.exe instance, hence the wrong PID?

          How about: args << "/c" << "cmd_ddl.bat"; ?

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          0
          • MasterBLBM Offline
            MasterBLBM Offline
            MasterBLB
            wrote on last edited by
            #5

            The same

            1 Reply Last reply
            0
            • MasterBLBM MasterBLB

              Sure thing:

              args << "/C" << "start" << "cmd_ddl.bat";
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @MasterBLB said in QProcess detection running or finished state - hints needed:

              args << "/C" << "start" << "cmd_ddl.bat";

              Just to reply to your first interrogation why PID seems to be wrong for you: adding "start" to launch the batch file will start "cmd_ddl.bat" in another process. You will get the PID for the "cmd.exe", but not for "cmd_ddm.bat"

              qint64 pid = 0;
              QProcess::startDetached("cmd", QStringList() << "/c" << "cmd_ddl.bat" , qApp->applicationDirPath() + "/Stuff", &pid);
              

              Shoud do what you want to do... now you have to be sure "cmd_ddl.bat" will finish!

              Regards

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

              1 Reply Last reply
              1
              • MasterBLBM Offline
                MasterBLBM Offline
                MasterBLB
                wrote on last edited by
                #7

                The pid is neither for cmd, nor for cmd_ddl.bat, nor ddl.exe (stuff run from the .bat)

                jsulmJ KroMignonK JonBJ 3 Replies Last reply
                0
                • MasterBLBM MasterBLB

                  The pid is neither for cmd, nor for cmd_ddl.bat, nor ddl.exe (stuff run from the .bat)

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @MasterBLB "now you have to be sure "cmd_ddl.bat" will finish!"

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

                  1 Reply Last reply
                  0
                  • MasterBLBM MasterBLB

                    The pid is neither for cmd, nor for cmd_ddl.bat, nor ddl.exe (stuff run from the .bat)

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

                    @MasterBLB said in QProcess detection running or finished state - hints needed:

                    The pid is neither for cmd, nor for cmd_ddl.bat, nor ddl.exe (stuff run from the .bat)

                    But are you sure you batch is finishing?
                    Do you have tried to kill it from Task Manager to see if signal is raised?

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

                    1 Reply Last reply
                    0
                    • MasterBLBM MasterBLB

                      The pid is neither for cmd, nor for cmd_ddl.bat, nor ddl.exe (stuff run from the .bat)

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

                      @MasterBLB
                      Could we (re-)start by discovering, given that you want to know if it is still running, why you want to use startDetached() rather than start()? Is it that your bat file/sub-process needs to request a (visible) console, or why?

                      1 Reply Last reply
                      0
                      • MasterBLBM Offline
                        MasterBLBM Offline
                        MasterBLB
                        wrote on last edited by
                        #11

                        Indeed, ddl.exe which is called from inside of the cmd_ddl.bat needs to have visible console whose in not closed immediately in case of error.

                        JonBJ 1 Reply Last reply
                        0
                        • MasterBLBM MasterBLB

                          Indeed, ddl.exe which is called from inside of the cmd_ddl.bat needs to have visible console whose in not closed immediately in case of error.

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

                          @MasterBLB
                          I do not know how the pid returned from startDetached() relates to that of the process spawned, and to detecting sub-process termination. Remember that says:

                          If the function is successful then *pid is set to the process identifier of the started process. Note that the child process may exit and the PID may become invalid without notice. Furthermore, after the child process exits, the same PID may be recycled and used by a completely different process.

                          If you really were to stick with startDetached(), you would probably need to use Windows EnumProcesses() to detect if your ddl.exe is still running or not. But...

                          ...Given that it is giving you problems, if you cannot resolve why don't you remove that complication by trying out start() instead? You can handle displaying an error message, if that's all ddl.exe needs a console for(?), from within Qt. If you just test it out, do you fare better with start() over startDetached()? You will not be using pid, just seeing whether or not you have received finished()/errorOccurred().

                          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