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. [SOLVED]Communicating application shutdown
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Communicating application shutdown

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 2.1k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    DRoscoe
    wrote on last edited by DRoscoe
    #1

    I have an application "A" and I am trying to communicate to application "B
    that "A" is shutting down. The following code shows what I am trying to send

    void TapDisplay::aboutToQuitApp()
    {
      // Send an EXIT command to Tap Engine
      TapUserEventMessage*
                    exit_message = new TapUserEventMessage(TapRequestAction::EXIT);
      emit outboundEngineData(exit_message);
    
      // This is where we clean up due to a signal from the main application that
      // we are shutting down
      if (engine_comm_handler)
      {
        engine_comm_handler->close();
      }
    }
    

    The problem is, I think the "A" is shutting down before the message has a chance to be sent to "B". The message is not being sent by "A", and thus "B" is unaware that "A" has gone away. The communication is occurring over a socket connection

    Is there a better way to do this, so that I have enough time for message propagation to "B" before pulling the plug on "A"?

    Thanks!
    -Dave

    1 Reply Last reply
    0
    • M Offline
      M Offline
      matze42
      wrote on last edited by
      #2

      Is application "B" in your hands as well ?

      If yes, when closing the socket properly, application "B" will get a socket error "QAbstractSocket::RemoteHostClosedError", when the socket is closed by any means by application "A".

      From your code this seems to be a sufficient information, cause there's not something like a "reason" - like "User pressed closed button" - transmitted.

      Or am I wrong ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DRoscoe
        wrote on last edited by
        #3

        I can't use the loss of socket connection as an exit trigger. Under normal operations, the connection can be lost. In that case I don't want the Application "B" to close. When a connection is re-established from application "A", data will resume flowing. Only willfully closing application "A" should send the exit signal to application "B". I need to have that EXIT message sent successfully.

        M 1 Reply Last reply
        0
        • D DRoscoe

          I can't use the loss of socket connection as an exit trigger. Under normal operations, the connection can be lost. In that case I don't want the Application "B" to close. When a connection is re-established from application "A", data will resume flowing. Only willfully closing application "A" should send the exit signal to application "B". I need to have that EXIT message sent successfully.

          M Offline
          M Offline
          ManhNT
          wrote on last edited by
          #4

          You can overide the method closeEvent of the widget.
          void MainWindow::closeEvent(QCloseEvent* event)
          {
          // your code here
          event->accept();
          }

          If you don't want it to close for some reasons, simply replace the accept() call with event->ignore();

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DRoscoe
            wrote on last edited by
            #5

            I am not using widgets, it's a QtQuick application.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by mcosta
              #6

              Hi,

              a solution could be send the message synchronously (without using signals)

              void TapDisplay::aboutToQuitApp()
              {
                // Send an EXIT command to Tap Engine
                TapUserEventMessage*
                              exit_message = new TapUserEventMessage(TapRequestAction::EXIT);
              
                // Some method that returns after the message is sent
                engine_comm_handler->sendMessage(exit_messsge);
              
                // This is where we clean up due to a signal from the main application that
                // we are shutting down
                if (engine_comm_handler)
                {
                  engine_comm_handler->close();
                }
              }
              

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              D 1 Reply Last reply
              1
              • M Offline
                M Offline
                matze42
                wrote on last edited by
                #7

                Do you have an application layer acknowledgement of messages in your protocol ?

                If yes, take the acknowledge of the exit message as a trigger for an exit signal, which may be connected to some slot calling exit on the application.
                Leads to the question, what to do, if the acknowledge timeouts ...

                If no, I guess that there's not any chance to know, whether the message arrived at application "B" or not.
                Even a waitForBytesWritten on the socket layer returns usually when the data are written to the internal TCP/IP buffer of the OS, and not when the data arrived the peer.

                1 Reply Last reply
                0
                • M mcosta

                  Hi,

                  a solution could be send the message synchronously (without using signals)

                  void TapDisplay::aboutToQuitApp()
                  {
                    // Send an EXIT command to Tap Engine
                    TapUserEventMessage*
                                  exit_message = new TapUserEventMessage(TapRequestAction::EXIT);
                  
                    // Some method that returns after the message is sent
                    engine_comm_handler->sendMessage(exit_messsge);
                  
                    // This is where we clean up due to a signal from the main application that
                    // we are shutting down
                    if (engine_comm_handler)
                    {
                      engine_comm_handler->close();
                    }
                  }
                  
                  D Offline
                  D Offline
                  DRoscoe
                  wrote on last edited by
                  #8

                  @mcosta Thank you! That seems to have mostly done the trick. I had to also add a call to flush() on the socket to make sure the transfer occurred. It's working fine now.

                  Thanks!

                  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