Unsolved startDetached with cmd how to close it automaticlly ??
-
I use startDetached to process cmd command.
When finished, I want to close it by itself.
I try to use "exit" at Main console which is useful.
But when i use it to startDetach.
It looks like unuseful.How could I close it ??
QString DOS( "cmd.exe" ); QStringList params1 = QStringList() << "/k" << "exit" ; //建立輸出dos字串 QProcess::startDetached( DOS, params1 ); //開始輸出文字
-
Hi
use /C instead to have close automatically? -
I try to fix it.
Looks like unuse. -
Stills stay open ?
-
@mrjj said in startDetached with cmd how to close it automaticlly ??:
Stills stay open ?
-
@RiceBall AFAIK (I'm using Windows seldom now), both
/c
and/k
are parameters that require a command afterwards. But there is no command in your code.May that be the problem?
-
Hi
I dont think its possible to close an already open cmd prompt ( keept open with K)
, by runner another cmd.exe.
Why not use /C in all places ? -
@mrjj said in startDetached with cmd how to close it automaticlly ??:
Hi
I dont think its possible to close an already open cmd prompt ( keept open with K)
, by runner another cmd.exe.
Why not use /C in all places ?How could I fix it by /C ??
Do I use it correctly ?? -
@mrjj said in startDetached with cmd how to close it automaticlly ??:
Hi
I dont think its possible to close an already open cmd prompt ( keept open with K)
, by runner another cmd.exe.
Why not use /C in all places ?QString DOS( "cmd.exe " ); foreach(QFileInfo mitm, mDir.entryInfoList(Qfilter)) { QStringList params = QStringList() << "/C" << "frtomem -p WnaYooAQ " << mitm.fileName() ; QProcess::startDetached( DOS, params ); }
I have tried to fix it by /C
But the problem is the same. -
@RiceBall Just tried on my Windows 10 machine with "dir" command - works as expected.
You're using QProcess wrongly.
It should be:QStringList params = QStringList() << "/C" << "frtomem" << "-p" << "WnaYooAQ" << mitm.fileName() ; QProcess::startDetached( DOS, params );
So, each parameter must be passed as a dedicated string, not all parameters in one string!
-
@jsulm said in startDetached with cmd how to close it automaticlly ??:
@RiceBall Just tried on my Windows 10 machine with "dir" command - works as expected.
You're using QProcess wrongly.
It should be:QStringList params = QStringList() << "/C" << "frtomem" << "-p" << "WnaYooAQ" << mitm.fileName() ; QProcess::startDetached( DOS, params );
So, each parameter must be passed as a dedicated string, not all parameters in one string!
I tried to fix it by your recommend.
But it also can't close by itself.
-
@RiceBall To me it looks like the command you're calling does not finish and that's why terminal window is not closed.
-
@jsulm said in startDetached with cmd how to close it automaticlly ??:
@RiceBall To me it looks like the command you're calling does not finish and that's why terminal window is not closed.
Could I set timer to close it forcely??
-
@RiceBall You should first check the documentation of that command. Maybe you just need to provide a parameter to it so it closes when finished.
If you want to abort it forcefully you should not use startDetached. -
@RiceBall are you sure, the terminal you're seeing is actually the one started by QProcess ?
I think you're writing a terminal app yourself. And you're calling a.exec(); on your QCoreApplication -> it's not going to close itself
-
@J.Hilk @jsulm
said in startDetached with cmd how to close it automaticlly ??:@RiceBall are you sure, the terminal you're seeing is actually the one started by QProcess ?
I think you're writing a terminal app yourself. And you're calling a.exec(); on your QCoreApplication -> it's not going to close itself
I try to simple it.
I fix it by ping 1.1.1.1 .
But it is also not success.int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString DOS( "cmd.exe " ); QStringList params = QStringList() << "/C" << "ping" << "1.1.1.1" ; QProcess::startDetached( DOS, params ); return a.exec(); }
If I can not use startDetached.
Do you have any recommend I can use ?? -
@RiceBall Is your app of type "console" (CONFIG += console in pro file)? If so then the terminal window you see is the one of your app and will be closed when you close your app. So, I don't see what the problem is in this case? @J-Hilk already pointed out this.
My widgets app does not show any terminal when I execute command like you do - because it is not a console app. -
@jsulm said in startDetached with cmd how to close it automaticlly ??:
@RiceBall Is your app of type "console" (CONFIG += console in pro file)? If so then the terminal window you see is the one of your app and will be closed when you close your app. So, I don't see what the problem is in this case? @J-Hilk already pointed out this.
My widgets app does not show any terminal when I execute command like you do - because it is not a console app.Sorry, Maybe I don't explain it very clear.
Actually,I make it by console.It is right.
I want to show some information for user.
When finished , I want to close APP or terminal window automatically. -
@RiceBall Yes, you did not explain well. Don't use startDetached (use the "normal" start()). Use https://doc.qt.io/qt-5/qprocess.html#finished signal and in the slot call https://doc.qt.io/qt-5/qcoreapplication.html#exit.