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
Forum Updated to NodeBB v4.3 + New Features

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.
  • C Offline
    C Offline
    Cobra91151
    wrote on 11 Mar 2018, 13:04 last edited by Cobra91151 3 Nov 2018, 13:04
    #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.

    A 1 Reply Last reply 11 Mar 2018, 13:18
    0
    • C Cobra91151
      11 Mar 2018, 13:04

      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.

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 11 Mar 2018, 13:18 last edited by
      #2

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

      C 1 Reply Last reply 11 Mar 2018, 13:27
      1
      • A aha_1980
        11 Mar 2018, 13:18

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

        C Offline
        C Offline
        Cobra91151
        wrote on 11 Mar 2018, 13:27 last edited by Cobra91151 3 Nov 2018, 14:12
        #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.

        A 1 Reply Last reply 11 Mar 2018, 15:10
        0
        • C Offline
          C Offline
          Cobra91151
          wrote on 11 Mar 2018, 15:08 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
          • C Cobra91151
            11 Mar 2018, 13:27

            @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.

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 11 Mar 2018, 15:10 last edited by aha_1980 3 Nov 2018, 15:11
            #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

            A 1 Reply Last reply 11 Mar 2018, 15:10
            0
            • A aha_1980
              11 Mar 2018, 15:10

              @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

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 11 Mar 2018, 15:10 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0

              1/6

              11 Mar 2018, 13:04

              • Login

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