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. Console app close issue

Console app close issue

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.5k Views
  • 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    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.

    aha_1980A 1 Reply Last reply
    0
    • Cobra91151C Cobra91151

      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.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Cobra91151 have you tried adding the parameter -n 4 to ping? that should cause ping to exit after 4 attempts.

      Cobra91151C 1 Reply Last reply
      1
      • aha_1980A aha_1980

        @Cobra91151 have you tried adding the parameter -n 4 to ping? that should cause ping to exit after 4 attempts.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        @aha_1980

        The problem here is something different, it loops data well but ctrl + c should stop it and display the statistics such as in cmd 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 from Qt 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:

        alt text

        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 press Ctrl + C the app closes but it should display statistics and stop executing ping. How to fix it? Thanks.

        aha_1980A 1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          1
          • Cobra91151C Cobra91151

            @aha_1980

            The problem here is something different, it loops data well but ctrl + c should stop it and display the statistics such as in cmd 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 from Qt 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:

            alt text

            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 press Ctrl + C the app closes but it should display statistics and stop executing ping. How to fix it? Thanks.

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #5

            @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

            aha_1980A 1 Reply Last reply
            0
            • aha_1980A aha_1980

              @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

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0

              • Login

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