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. Run process in background without blocking
Forum Updated to NodeBB v4.3 + New Features

Run process in background without blocking

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 9.3k 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.
  • R Offline
    R Offline
    rul3s
    wrote on last edited by rul3s
    #1

    Hi!
    I'm developing a software in QT that runs only in windows.
    This software has to run execute another software, a process that runs from CMD and creates a tunnel, so it stays running until Ctrl-C is pressed or send a taskkill signal.

    I tried to run it with startDetached and with system but with startDetached() it doesn't work, it's just like it's not being executed, and with system() it's being executed but it blocks the GUI thread.

    I've thinked about creating a new thread and then execute system() from there but I think it's not the best idea...

    How would you do this?

    CODE:

    bool MotecDesktop::startNgrok(){
        qDebug() << "Entered startNgrok";
        QProcess ngrok;
    
        QDir::setCurrent(rootPath + ressources);
        
        ngrok.startDetached(rootPath + ressources +"ngrok tcp 52125"); //Option 1, not running
        system("ngrok tcp 52125");       //Option 2, blocking UI thread
        
        return true;
    }
    
    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to the forums
      Could you try to use it properly and not create one string but use the methods of setting the parameters as seen in docs?
      http://doc.qt.io/qt-5/qprocess.html

      bool QProcess::startDetached(qint64 *pid = nullptr)
      Starts the program set by setProgram() with arguments set by setArguments() in a new process, and detaches from it. 
      
      R 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi and welcome to the forums
        Could you try to use it properly and not create one string but use the methods of setting the parameters as seen in docs?
        http://doc.qt.io/qt-5/qprocess.html

        bool QProcess::startDetached(qint64 *pid = nullptr)
        Starts the program set by setProgram() with arguments set by setArguments() in a new process, and detaches from it. 
        
        R Offline
        R Offline
        rul3s
        wrote on last edited by
        #3

        @mrjj said in Run process in background without blocking:

        Hi and welcome to the forums
        Could you try to use it properly and not create one string but use the methods of setting the parameters as seen in docs?
        http://doc.qt.io/qt-5/qprocess.html

        bool QProcess::startDetached(qint64 *pid = nullptr)
        Starts the program set by setProgram() with arguments set by setArguments() in a new process, and detaches from it. 
        

        Thanks for your answer mrjj, I've tried it with a only String because is one of the options in the editor.
        By the way, I've just tried like this and same thing is happening, just like the process is not being executed.

        bool MotecDesktop::startNgrok(){
            qDebug() << "Entered startNgrok";
            QProcess ngrok;
            QStringList arguments;
            QDir::setCurrent(rootPath + ressources);
        
            arguments.append("tcp");
            arguments.append("55125");
            ngrok.setProgram("ngrok.exe");
            ngrok.setArguments(arguments);
            ngrok.startDetached();
        
            //system("ngrok tcp 52125");
        
            QDir::setCurrent(rootPath);
            return true;
        }
        

        Any idea?
        In Unix I would only do system("ngrok tcp 55123 &") and I think it should work.

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

          Then don't use startDetached and check the return value, error codes and stdout/stderr of the QProcess object.

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

          R 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            Then don't use startDetached and check the return value, error codes and stdout/stderr of the QProcess object.

            R Offline
            R Offline
            rul3s
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in Run process in background without blocking:

            Then don't use startDetached and check the return value, error codes and stdout/stderr of the QProcess object.

            Which method I should use instead of startDetached so?

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

              @rul3s said in Run process in background without blocking:

              Which method I should use instead of startDetached so?

              Isn't the documentation clear enough? http://doc.qt.io/qt-5/qprocess.html#details

              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
              1
              • R rul3s

                Hi!
                I'm developing a software in QT that runs only in windows.
                This software has to run execute another software, a process that runs from CMD and creates a tunnel, so it stays running until Ctrl-C is pressed or send a taskkill signal.

                I tried to run it with startDetached and with system but with startDetached() it doesn't work, it's just like it's not being executed, and with system() it's being executed but it blocks the GUI thread.

                I've thinked about creating a new thread and then execute system() from there but I think it's not the best idea...

                How would you do this?

                CODE:

                bool MotecDesktop::startNgrok(){
                    qDebug() << "Entered startNgrok";
                    QProcess ngrok;
                
                    QDir::setCurrent(rootPath + ressources);
                    
                    ngrok.startDetached(rootPath + ressources +"ngrok tcp 52125"); //Option 1, not running
                    system("ngrok tcp 52125");       //Option 2, blocking UI thread
                    
                    return true;
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @rul3s
                Yet another case here of people wanting to run startDetached() for no apparent reason!

                It sounds like you just want to use QProcess::start(). This starts/runs your sub-process without in any way blocking your calling GUI thread. (In effect, start() already does the & you have put at the end of your Linux command-line.) You can get notifications from the sub-process (e.g. when it exits) via the signals that emits to slots you define, while your GUI loop continues to run.

                until Ctrl-C is pressed

                Hmm, where do you think you're going to press this Ctrl+C, such that it affects the sub-process? Does your ngrok command open its own console/terminal window, or what?

                R 1 Reply Last reply
                3
                • JonBJ JonB

                  @rul3s
                  Yet another case here of people wanting to run startDetached() for no apparent reason!

                  It sounds like you just want to use QProcess::start(). This starts/runs your sub-process without in any way blocking your calling GUI thread. (In effect, start() already does the & you have put at the end of your Linux command-line.) You can get notifications from the sub-process (e.g. when it exits) via the signals that emits to slots you define, while your GUI loop continues to run.

                  until Ctrl-C is pressed

                  Hmm, where do you think you're going to press this Ctrl+C, such that it affects the sub-process? Does your ngrok command open its own console/terminal window, or what?

                  R Offline
                  R Offline
                  rul3s
                  wrote on last edited by rul3s
                  #8

                  @Christian-Ehrlicher said in Run process in background without blocking:

                  @rul3s said in Run process in background without blocking:

                  Which method I should use instead of startDetached so?

                  Isn't the documentation clear enough? http://doc.qt.io/qt-5/qprocess.html#details

                  If I'm asking is because, for me, it's not clear enought or I am missing something, I'm not asking for fun.

                  @JonB said in Run process in background without blocking:

                  @rul3s
                  Yet another case here of people wanting to run startDetached() for no apparent reason!

                  It sounds like you just want to use QProcess::start(). This starts/runs your sub-process without in any way blocking your calling GUI thread. (In effect, start() already does the & you have put at the end of your Linux command-line.) You can get notifications from the sub-process (e.g. when it exits) via the signals that emits to slots you define, while your GUI loop continues to run.

                  until Ctrl-C is pressed

                  Hmm, where do you think you're going to press this Ctrl+C, such that it affects the sub-process? Does your ngrok command open its own console/terminal window, or what?

                  As I said in my first message, with the QProcess::start() I was blocking the GUI of my software, so I was thinking that here was the problem.
                  About the Ctrl+C obviusly I'm not going to do that, I was going to execute taskkill to kill the process when I don't need it anymore.

                  I'm going to investigate a little bit more about why QProcess::start() is blocking my software if it should not do it.

                  Thanks.

                  JonBJ 1 Reply Last reply
                  0
                  • R rul3s

                    @Christian-Ehrlicher said in Run process in background without blocking:

                    @rul3s said in Run process in background without blocking:

                    Which method I should use instead of startDetached so?

                    Isn't the documentation clear enough? http://doc.qt.io/qt-5/qprocess.html#details

                    If I'm asking is because, for me, it's not clear enought or I am missing something, I'm not asking for fun.

                    @JonB said in Run process in background without blocking:

                    @rul3s
                    Yet another case here of people wanting to run startDetached() for no apparent reason!

                    It sounds like you just want to use QProcess::start(). This starts/runs your sub-process without in any way blocking your calling GUI thread. (In effect, start() already does the & you have put at the end of your Linux command-line.) You can get notifications from the sub-process (e.g. when it exits) via the signals that emits to slots you define, while your GUI loop continues to run.

                    until Ctrl-C is pressed

                    Hmm, where do you think you're going to press this Ctrl+C, such that it affects the sub-process? Does your ngrok command open its own console/terminal window, or what?

                    As I said in my first message, with the QProcess::start() I was blocking the GUI of my software, so I was thinking that here was the problem.
                    About the Ctrl+C obviusly I'm not going to do that, I was going to execute taskkill to kill the process when I don't need it anymore.

                    I'm going to investigate a little bit more about why QProcess::start() is blocking my software if it should not do it.

                    Thanks.

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

                    @rul3s
                    QProcess::start() in itself definitely should not be blocking --- it's just a fork followed by an exec, and should be "fast". It does not wait for anything --- it sets off (initiates) the sub-process and then immediately continues in the parent from where it was called for (you don't use QProcess::waitFor...() after it, do you?). However, I'm a little unclear on what the behaviour might appear to be depending on just what your sub-process itself might do.

                    What does ngrok itself do? E.g. is it command-line or does it have a UI or does it create its own terminal/console or what?

                    R 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @rul3s
                      QProcess::start() in itself definitely should not be blocking --- it's just a fork followed by an exec, and should be "fast". It does not wait for anything --- it sets off (initiates) the sub-process and then immediately continues in the parent from where it was called for (you don't use QProcess::waitFor...() after it, do you?). However, I'm a little unclear on what the behaviour might appear to be depending on just what your sub-process itself might do.

                      What does ngrok itself do? E.g. is it command-line or does it have a UI or does it create its own terminal/console or what?

                      R Offline
                      R Offline
                      rul3s
                      wrote on last edited by
                      #10

                      @JonB said in Run process in background without blocking:

                      @rul3s
                      QProcess::start() in itself definitely should not be blocking --- it's just a fork followed by an exec, and should be "fast". It does not wait for anything --- it sets off (initiates) the sub-process and then immediately continues in the parent from where it was called for (you don't use QProcess::waitFor...() after it, do you?). However, I'm a little unclear on what the behaviour might appear to be depending on just what your sub-process itself might do.

                      What does ngrok itself do? E.g. is it command-line or does it have a UI or does it create its own terminal/console or what?

                      Hi JonB,
                      On my first attempts I was trying, unsusccessfully with QProcess::start(), then I managed to QProcess::startDetached() and finally to system().

                      Now, I just tried again with QProcess::start() worked fine, I don't know what was going wrong.... but now it's solved.

                      Thanks all for your help.

                      bool MotecDesktop::startNgrok(){
                          QString program = rootPath + ressources + "ngrok.exe";
                          QStringList arguments;
                          QProcess *myProcess = new QProcess();
                          
                          arguments << "tcp" << "54321";
                          myProcess->start(program, arguments);
                      
                          return true;
                      }
                      
                      1 Reply Last reply
                      2

                      • Login

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