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 doesn't stop when the command is finished
Forum Updated to NodeBB v4.3 + New Features

QProcess doesn't stop when the command is finished

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.2k Views 2 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.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hi, I'm trying to get a live output out of a running thread, but I have a problem. When the command is supposed to finish, QProcess is still running. When I tried to run it without the live output, it worked fine.
    Here's what I'm working with:

    QProcess fd;
    fd.setWorkingDirectory(dir + "platform-tools");
    QStringList command; command << "fastboot devices";
    fd.startDetached("powershell", command);
    while(fd.Running) { //without this while it works fine
        qDebug() << fd.readAll();
        msleep(100);
    }
    

    After the command is "finished", the output log prints out QIODevice::read (QProcess): device not open.

    JonBJ 1 Reply Last reply
    0
    • S Sucharek

      Hi, I'm trying to get a live output out of a running thread, but I have a problem. When the command is supposed to finish, QProcess is still running. When I tried to run it without the live output, it worked fine.
      Here's what I'm working with:

      QProcess fd;
      fd.setWorkingDirectory(dir + "platform-tools");
      QStringList command; command << "fastboot devices";
      fd.startDetached("powershell", command);
      while(fd.Running) { //without this while it works fine
          qDebug() << fd.readAll();
          msleep(100);
      }
      

      After the command is "finished", the output log prints out QIODevice::read (QProcess): device not open.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Sucharek

      I'm trying to get a live output out of a running thread,

      Where is there a "thread" involved anywhere here?

      Don't use QProcess::startDetached() if you want to wait for a subprocess to finish. Don't use it if you want to read output. You are also incorrectly using a static method when you think you are using an instance method.

      while(fd.Running)

      Please show me in the documentation where QProcess has a member variable named Running? It does not. There is an enum QProcess::ProcessState which has a value of 2. So your while never exits.

      msleep(100);

      Don't do this.

      So basically you need to completely rewrite your code. There are plenty examples out there.

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @Sucharek

        I'm trying to get a live output out of a running thread,

        Where is there a "thread" involved anywhere here?

        Don't use QProcess::startDetached() if you want to wait for a subprocess to finish. Don't use it if you want to read output. You are also incorrectly using a static method when you think you are using an instance method.

        while(fd.Running)

        Please show me in the documentation where QProcess has a member variable named Running? It does not. There is an enum QProcess::ProcessState which has a value of 2. So your while never exits.

        msleep(100);

        Don't do this.

        So basically you need to completely rewrite your code. There are plenty examples out there.

        S Offline
        S Offline
        Sucharek
        wrote on last edited by Sucharek
        #3

        Hi @JonB, I wrote it wrong. I meant get live output out of that QProcess.
        I used startDeatched(), because I thought I could get live output somehow.

        I changed it to this:

        ... above still the same ...
        fd.start("powershell", command);
        while(fd.state() == QProcess::Running) {
            qDebug() << fd.readAll();
        }
        fd.waitForFinished(); //not sure where to put this
        

        That also doesn't work. It just prints "". If I put waitForFinished() before the while loop, it doesn't even activate (because it starts after the process finishes).
        Edit: If I put waitForFinished() inside the while loop, it kinda works, but the problem is, it doesn't output anything and runs only once.

        JonBJ 1 Reply Last reply
        0
        • S Sucharek

          Hi @JonB, I wrote it wrong. I meant get live output out of that QProcess.
          I used startDeatched(), because I thought I could get live output somehow.

          I changed it to this:

          ... above still the same ...
          fd.start("powershell", command);
          while(fd.state() == QProcess::Running) {
              qDebug() << fd.readAll();
          }
          fd.waitForFinished(); //not sure where to put this
          

          That also doesn't work. It just prints "". If I put waitForFinished() before the while loop, it doesn't even activate (because it starts after the process finishes).
          Edit: If I put waitForFinished() inside the while loop, it kinda works, but the problem is, it doesn't output anything and runs only once.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @Sucharek said in QProcess doesn't stop when the command is finished:

          while(fd.state() == QProcess::Running)

          You should not do this either. No loops anywhere!

          What is wrong with:

          fd.start("powershell", command);
          fd.waitForFinished(); 
          qDebug() << fs.readAllStrandardOutput();
          qDebug() << fs.readAllStrandardError();
          

          ?

          What does "I meant get live output out of that QProcess." mean? If you want output while it is running (instead of at the end) you must use signals & slots instead of waitForFinished(). Whether you will actually get output as it goes along depends on whether any output from powershell is buffered at its side, which you cannot do anything about.

          1 Reply Last reply
          3
          • alik_coA Offline
            alik_coA Offline
            alik_co
            wrote on last edited by
            #5

            To expand what @JonB said, here is working code

            void runPowerShellCommand()
            {
                // Console app, using puts().
                QProcess command;
                command.start("powershell.exe", QStringList("ls"));
                if (!command.waitForStarted(2000))
                {
                    puts("Failed to start command");
                    exit(101);
                }
                if (!command.waitForFinished(5000))
                {
                    puts("Command hang");
                    command.terminate();
                    exit(102);
                }
                puts(command.readAllStandardOutput().constData());
            }
            
            
            1 Reply Last reply
            2
            • faduF Offline
              faduF Offline
              fadu
              wrote on last edited by
              #6

              @Sucharek
              hi
              you can try this

              QString Adb::cmd(const QString &command)
              {
                  QProcess P2;
                  P2.start(command);
                  P2.waitForFinished(-1);
                  P2.setReadChannel(QProcess::StandardOutput);
                  QTextStream reade2(&P2);
                  QString line2,line,Out;
                  while (reade2.readLineInto(&line2))
                      Out.append(line2 +'\n');
                  P2.setReadChannel(QProcess::StandardError);
                  QTextStream reader(&P2);
                  while (reader.readLineInto(&line))
                      Out.append(line +'\n');
                  P2.close();
                  return Out.trimmed();
              }
              

              then when you need output

              qDebug()<< cmd("fastboot devices")
              

              or in QString

              QString output =  cmd("fastboot devices")
              
              1 Reply Last reply
              1
              • S Offline
                S Offline
                Sucharek
                wrote on last edited by
                #7

                Thanks for all your replies.
                I'm gonna try the codes. Thanks

                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