Console app close issue
-
Hi! I want to read some ping information, the problem is the app closes after
ctrl + c
shortcut is activated, so I can't read any data.#include <QCoreApplication> #include <QDebug> #include <QProcess> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QProcess::execute("ping.exe", QStringList() << "8.8.8.8" << "-t"); system("Pause"); return a.exec(); }
system("Pause")
function doesn't work in this case. Any ideas? Thanks. -
Hi! I want to read some ping information, the problem is the app closes after
ctrl + c
shortcut is activated, so I can't read any data.#include <QCoreApplication> #include <QDebug> #include <QProcess> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QProcess::execute("ping.exe", QStringList() << "8.8.8.8" << "-t"); system("Pause"); return a.exec(); }
system("Pause")
function doesn't work in this case. Any ideas? Thanks.@Cobra91151 have you tried adding the parameter
-n 4
to ping? that should causeping
to exit after 4 attempts. -
@Cobra91151 have you tried adding the parameter
-n 4
to ping? that should causeping
to exit after 4 attempts.The problem here is something different, it loops data well but
ctrl + c
should stop it and display the statistics such as incmd
but it exits the app instead.It seems that when
ping.exe
is closed, it also closes my app. The issue is present when run the app from the directory not fromQt Creator
.I have changed code to:
QProcess pingProcess; QObject::connect(&pingProcess, &QProcess::readyReadStandardOutput, [&pingProcess]() { qDebug() << pingProcess.readAllStandardOutput().trimmed(); }); QObject::connect(&pingProcess, &QProcess::readyReadStandardError, [&pingProcess]() { qDebug() << pingProcess.readAllStandardError().trimmed(); }); QObject::connect(&pingProcess, static_cast<void (QProcess::*)(int)>(&QProcess::finished), [=]() { system("Pause"); }); pingProcess.startDetached("ping.exe", QStringList() << "8.8.8.8" << "-t"); pingProcess.waitForFinished();
It works well but when execute from the directory it closes after the
ctrl + c
and no stats available.Screenshot:
Info:
Ping the specified host until stopped. To see statistics and continue - type Control-Break; To stop - type Control-C.
So when I press
ctrl + break
the app displays the stats and continue working. When I pressCtrl + C
the app closes but it should display statistics and stop executing ping. How to fix it? Thanks. -
It seems that this is the default behavior by
ping.exe
, so I decided to change my app to get the ping loop number specified by a user.Code:
int count = 0; cout << "Enter number to proceed the ping loop (0 to exit): "; cin >> count; if (count >= 1) { QProcess pingProcess; pingProcess.execute("ping.exe", QStringList() << "8.8.8.8" << "-n" << QString::number(count)); cout << endl; system("Pause"); } return 0;
And now it works well.
-
The problem here is something different, it loops data well but
ctrl + c
should stop it and display the statistics such as incmd
but it exits the app instead.It seems that when
ping.exe
is closed, it also closes my app. The issue is present when run the app from the directory not fromQt Creator
.I have changed code to:
QProcess pingProcess; QObject::connect(&pingProcess, &QProcess::readyReadStandardOutput, [&pingProcess]() { qDebug() << pingProcess.readAllStandardOutput().trimmed(); }); QObject::connect(&pingProcess, &QProcess::readyReadStandardError, [&pingProcess]() { qDebug() << pingProcess.readAllStandardError().trimmed(); }); QObject::connect(&pingProcess, static_cast<void (QProcess::*)(int)>(&QProcess::finished), [=]() { system("Pause"); }); pingProcess.startDetached("ping.exe", QStringList() << "8.8.8.8" << "-t"); pingProcess.waitForFinished();
It works well but when execute from the directory it closes after the
ctrl + c
and no stats available.Screenshot:
Info:
Ping the specified host until stopped. To see statistics and continue - type Control-Break; To stop - type Control-C.
So when I press
ctrl + break
the app displays the stats and continue working. When I pressCtrl + C
the app closes but it should display statistics and stop executing ping. How to fix it? Thanks.@Cobra91151 said in Console app close issue:
pingProcess.startDetached("ping.exe", QStringList() << "8.8.8.8" << "-t");
I think your problem is the
"-t"
parameter. What happens if you replace that with"-n" << "4"
? Because with "-t" ping never stops, while with "-n 4" it only pings four times.Disclaimer: I cannot test as I don't have Windows at hand...
Edit: Ah, I see you got it. So please close this thread as SOLVED. Thanks
-
@Cobra91151 said in Console app close issue:
pingProcess.startDetached("ping.exe", QStringList() << "8.8.8.8" << "-t");
I think your problem is the
"-t"
parameter. What happens if you replace that with"-n" << "4"
? Because with "-t" ping never stops, while with "-n 4" it only pings four times.Disclaimer: I cannot test as I don't have Windows at hand...
Edit: Ah, I see you got it. So please close this thread as SOLVED. Thanks
This post is deleted!