Process and child process
-
@jsulm To be honest, probably because the example I came across for QProcess was using it. What I want is to launch the child process and allow the launching process to carry on without being held up.
-
@jsulm To be honest, probably because the example I came across for QProcess was using it. What I want is to launch the child process and allow the launching process to carry on without being held up.
@SPlatten said in Process and child process:
What I want is to launch the child process and allow the launching process to carry on without being held up.
That happens without
startDetached(),QProcessruns asynchronously. You only needstartDetachedbasically if you want to sub-process not to e.g. get terminated when your process exits. -
@SPlatten said in Process and child process:
What I want is to launch the child process and allow the launching process to carry on without being held up.
That happens without
startDetached(),QProcessruns asynchronously. You only needstartDetachedbasically if you want to sub-process not to e.g. get terminated when your process exits. -
-
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
-
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
@SPlatten said in Process and child process:
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
well, Process has the started and stateChanged signal for that.
https://doc.qt.io/qt-5/qprocess.html#startedyou should actually listen to the stateChanged signal, to see if it changes from Running to NotRunning
-
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
@SPlatten
You have other things you can look at instead, likeQProcess::stateChangedsignal for clues.While you get it working, you may find
start()is easier to work with thanstartDetached().EDIT Sigh, looks like 3 of us are all trying to answer :)
If it's not secret, you might like to share with us the full command you are running, including the arguments, in case we can spot anything for you....
-
3 People 3 times the same thought
Up Top🙌
-
@SPlatten
You have other things you can look at instead, likeQProcess::stateChangedsignal for clues.While you get it working, you may find
start()is easier to work with thanstartDetached().EDIT Sigh, looks like 3 of us are all trying to answer :)
If it's not secret, you might like to share with us the full command you are running, including the arguments, in case we can spot anything for you....
@JonB I just tried start and it didn't work, I got errors in the Application Output:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QProcess(0x101e0b420), parent's thread is QThread(0x101e0b880), current thread is clsThread(0x122c1bcc0) -
@JonB I just tried start and it didn't work, I got errors in the Application Output:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QProcess(0x101e0b420), parent's thread is QThread(0x101e0b880), current thread is clsThread(0x122c1bcc0) -
@SPlatten said in Process and child process:
Why does startDetached take a qint64 for the PID as the last parameter but the QProcess function pid returns not a qint64 but a Q_PID?
Why are these different types?Don't use
Q_PIDfor this (Windows). Useqint64 QProcess::processId() const. -
@JonB I just tried start and it didn't work, I got errors in the Application Output:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QProcess(0x101e0b420), parent's thread is QThread(0x101e0b880), current thread is clsThread(0x122c1bcc0) -
@SPlatten said in Process and child process:
QObject: Cannot create children for a parent that is in a different thread.
That would explain a lot...! :)
-
@JonB, I think I said from the start these are two different processes, not threads in the same process.
@SPlatten said in Process and child process:
I think I said from the start these are two different processes
This is clear.
I asked because the warning you posted comes when using threads. -
@SPlatten
You have other things you can look at instead, likeQProcess::stateChangedsignal for clues.While you get it working, you may find
start()is easier to work with thanstartDetached().EDIT Sigh, looks like 3 of us are all trying to answer :)
If it's not secret, you might like to share with us the full command you are running, including the arguments, in case we can spot anything for you....
-
@JonB I tried start and it didn't work at all, so I'm back to startDetached which does launch the application and I can see it creates a log file then disappears.
@J-Hilk , @JonB , @jsulm, I've modified the function:
bool clsMainWnd::blnLaunch(QString strApp, QStringList& slstArgs, qint64& int64PID) { Q_ASSERT_X(clsMainWnd::mspobjProcess!=nullptr, "blnLaunch", "mspobjProcess is null!"); QString strFullPath(clsDebugService::strGetUserFolder(strApp)); int64PID = 0; qdbg() << "Checking for PID for: " << strFullPath; if ( blnGetPID(strFullPath, int64PID) == true ) { //Process already running, no action required } else { int intLastSep = strFullPath.lastIndexOf(QDir::separator()); if ( intLastSep > 0 ) { QString strName = strFullPath.mid(intLastSep + 1) ,strPath = strFullPath.mid(0, intLastSep + 1); qdbg() << "Launching: " << strFullPath; clsMainWnd::mspobjProcess->setArguments(slstArgs); clsMainWnd::mspobjProcess->setWorkingDirectory(strPath); clsMainWnd::mspobjProcess->setProgram(strName); clsMainWnd::mspobjProcess->startDetached(&int64PID); } } if ( int64PID > 0 ) { qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID); return true; } return false; }I can see that a valid PID is returned and that the log files are created, but when I check for the process:
ps -AThe PID isn't present.
-
@J-Hilk , @JonB , @jsulm, I've modified the function:
bool clsMainWnd::blnLaunch(QString strApp, QStringList& slstArgs, qint64& int64PID) { Q_ASSERT_X(clsMainWnd::mspobjProcess!=nullptr, "blnLaunch", "mspobjProcess is null!"); QString strFullPath(clsDebugService::strGetUserFolder(strApp)); int64PID = 0; qdbg() << "Checking for PID for: " << strFullPath; if ( blnGetPID(strFullPath, int64PID) == true ) { //Process already running, no action required } else { int intLastSep = strFullPath.lastIndexOf(QDir::separator()); if ( intLastSep > 0 ) { QString strName = strFullPath.mid(intLastSep + 1) ,strPath = strFullPath.mid(0, intLastSep + 1); qdbg() << "Launching: " << strFullPath; clsMainWnd::mspobjProcess->setArguments(slstArgs); clsMainWnd::mspobjProcess->setWorkingDirectory(strPath); clsMainWnd::mspobjProcess->setProgram(strName); clsMainWnd::mspobjProcess->startDetached(&int64PID); } } if ( int64PID > 0 ) { qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID); return true; } return false; }I can see that a valid PID is returned and that the log files are created, but when I check for the process:
ps -AThe PID isn't present.
@SPlatten
As we said before, if you want help on this you really need to put in more debugging information calls. Like slots forerrorOccurred,finished,started,stateChanged, and reading of anything appearing on stdout/stderr, e.g. viareadyReadStandardError/Output. And there is theexitCode(). Sorry, but that's how it is. I also offered to look for clues if you wanted to tell us the program you are executing and what arguments you pass , but you didn't reply yea or nay.And btw you have something fishy going on if you can run this command via
startDetached()but not viastart().And one other thing: the fact that you can't find the pid in itself proves nothing. It might be running fine. Processes can spawn sub-processes and exit.
-
@SPlatten
As we said before, if you want help on this you really need to put in more debugging information calls. Like slots forerrorOccurred,finished,started,stateChanged, and reading of anything appearing on stdout/stderr, e.g. viareadyReadStandardError/Output. And there is theexitCode(). Sorry, but that's how it is. I also offered to look for clues if you wanted to tell us the program you are executing and what arguments you pass , but you didn't reply yea or nay.And btw you have something fishy going on if you can run this command via
startDetached()but not viastart().And one other thing: the fact that you can't find the pid in itself proves nothing. It might be running fine. Processes can spawn sub-processes and exit.
-
@JonB , I've connected up slots to the following signals:
connect(clsMainWnd::mspobjProcess, &QProcess::errorOccurred, this, &clsMainWnd::qprocErrorOccurred); connect(clsMainWnd::mspobjProcess, &QProcess::started, this, &clsMainWnd::qprocStarted); connect(clsMainWnd::mspobjProcess, &QProcess::stateChanged, this, &clsMainWnd::qprocStateChanged);The implementation:
void clsMainWnd::qprocErrorOccurred(QProcess::ProcessError error) { qdbg() << "QProcess errorOccurred:" << error; } void clsMainWnd::qprocStarted() { qdbg() << "QProcess started"; } void clsMainWnd::qprocStateChanged(QProcess::ProcessState newState) { qdbg() << "QProcess stateChanged, newState:" << newState; }I also put breakpoints in each slot, none of these got triggered. I'm not surprised though because I'm not suggesting the problem is with QProcess. I'm just trying to find out why the process when started this way is terminating where as if I launch it in Qt Creator it continues to run.
I would have though the "started" and "stateChanged" signals would get raised, but I don't get anything.