Unsolved Run process in background without blocking
-
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; }
-
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.htmlbool QProcess::startDetached(qint64 *pid = nullptr) Starts the program set by setProgram() with arguments set by setArguments() in a new process, and detaches from it.
-
@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.htmlbool 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. -
Then don't use startDetached and check the return value, error codes and stdout/stderr of the QProcess object.
-
@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?
-
@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
-
@rul3s
Yet another case here of people wanting to runstartDetached()
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? -
@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 runstartDetached()
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.
-
@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 useQProcess::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? -
@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 useQProcess::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 withQProcess::start()
, then I managed toQProcess::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; }