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. QNetworkAccessManager object not receiving the response when closing application

QNetworkAccessManager object not receiving the response when closing application

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 843 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.
  • A Offline
    A Offline
    AbhiVarma
    wrote on last edited by
    #1

    Hi Qt Community, I have a desktop application written in qt and a server application thats written in .Net core. I'm trying to make an api call when I'm closing my application. Server is getting my call, processing it and sending back the response but my QNetworkAccessManager object is not emitting the finished signal. Other api call that are made when using the application are working fine, Only when user click the X button to close the app at that time whatever api calls i make are not getting the response. I have checked all the ports and signal slots are in connected state and server is sending me the response. Below is the portion of the code that shows the flow.

    App.h

    enum class  RESPONSE_STATE {
     
            RESPONSE_RECEIVED_STATE = 0,
            RESPONSE_REQUESTED_STATE,
            RESPONSE_INVALID   
    
    }
    
    class CApp : public QtSingleApplication
    {
        Q_OBJECT
        private: 
                  //member variables
                  RESPONSE_STATE m_eApiCallResponseState;
                  QNetworkAccessManager* m_pNetworkManager;
    
        public:
                  CApp( );
                  ~CApp();
    
        public slots:
                  void OnClosing();
                  void OnResponseReceived(QNetworkReply* pReply);     
    };
    

    App.cpp

    CApp::CApp ()
                  : m_eApiCallResponseState(RESPONSE_STATE ::RESPONSE_INVALID)
                  ,  m_pNetworkManager(new QNetworkAccessManager(this)
    {
       m_pNetworkManager->setProxy(QNetworkProxy::NoProxy);
       connect(m_pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(OnResponseReceived(QNetworkReply*)));
       connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onClosing()));
    }
    
    CApp::~CApp()
    {
       // Disconnect all the signal slot connection
       // Close all the ports
       // Delete member variables
    }
    
    void CApp::onClosing()
    {
        m_eApiCallResponseState = RESPONSE_REQUESTED_STATE;
        //I'm making api call here using m_pNetworkManager
        do
        {
        // Checked port is still connected
        }
        while(m_eApiCallResponseState  != RESPONSE_REQUESTED_STATE);
    }
    
    void CApp::OnResponseReceived(QNetworkReply* pReply)
    {
       //Handle the response
      m_eApiCallResponseState  = RESPONSE_RECEIVED_STATE ;
    }
    

    I have made the same api call from other place im getting the response. On making the call from the onClosing(). m_pNetworkManager is not emitting the finished() signal. introduced do while loop just to make sure response is coming or not. Any help is much appreciated.

    jsulmJ 1 Reply Last reply
    0
    • A AbhiVarma

      Hi Qt Community, I have a desktop application written in qt and a server application thats written in .Net core. I'm trying to make an api call when I'm closing my application. Server is getting my call, processing it and sending back the response but my QNetworkAccessManager object is not emitting the finished signal. Other api call that are made when using the application are working fine, Only when user click the X button to close the app at that time whatever api calls i make are not getting the response. I have checked all the ports and signal slots are in connected state and server is sending me the response. Below is the portion of the code that shows the flow.

      App.h

      enum class  RESPONSE_STATE {
       
              RESPONSE_RECEIVED_STATE = 0,
              RESPONSE_REQUESTED_STATE,
              RESPONSE_INVALID   
      
      }
      
      class CApp : public QtSingleApplication
      {
          Q_OBJECT
          private: 
                    //member variables
                    RESPONSE_STATE m_eApiCallResponseState;
                    QNetworkAccessManager* m_pNetworkManager;
      
          public:
                    CApp( );
                    ~CApp();
      
          public slots:
                    void OnClosing();
                    void OnResponseReceived(QNetworkReply* pReply);     
      };
      

      App.cpp

      CApp::CApp ()
                    : m_eApiCallResponseState(RESPONSE_STATE ::RESPONSE_INVALID)
                    ,  m_pNetworkManager(new QNetworkAccessManager(this)
      {
         m_pNetworkManager->setProxy(QNetworkProxy::NoProxy);
         connect(m_pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(OnResponseReceived(QNetworkReply*)));
         connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onClosing()));
      }
      
      CApp::~CApp()
      {
         // Disconnect all the signal slot connection
         // Close all the ports
         // Delete member variables
      }
      
      void CApp::onClosing()
      {
          m_eApiCallResponseState = RESPONSE_REQUESTED_STATE;
          //I'm making api call here using m_pNetworkManager
          do
          {
          // Checked port is still connected
          }
          while(m_eApiCallResponseState  != RESPONSE_REQUESTED_STATE);
      }
      
      void CApp::OnResponseReceived(QNetworkReply* pReply)
      {
         //Handle the response
        m_eApiCallResponseState  = RESPONSE_RECEIVED_STATE ;
      }
      

      I have made the same api call from other place im getting the response. On making the call from the onClosing(). m_pNetworkManager is not emitting the finished() signal. introduced do while loop just to make sure response is coming or not. Any help is much appreciated.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @AbhiVarma You should override closeEvent like shown in https://stackoverflow.com/questions/17480984/qt-how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button and delay the closing. Else your app is closed before it gets the response.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      2
      • jsulmJ jsulm

        @AbhiVarma You should override closeEvent like shown in https://stackoverflow.com/questions/17480984/qt-how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button and delay the closing. Else your app is closed before it gets the response.

        A Offline
        A Offline
        AbhiVarma
        wrote on last edited by AbhiVarma
        #3

        @jsulm I have tried to call the api call from MainWindow closeEvent before accepting the event but still im not getting the response

        Do you have any suggested method for debugging the network manager.

        jsulmJ 1 Reply Last reply
        0
        • A AbhiVarma

          @jsulm I have tried to call the api call from MainWindow closeEvent before accepting the event but still im not getting the response

          Do you have any suggested method for debugging the network manager.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @AbhiVarma said in QNetworkAccessManager object not receiving the response when closing application:

          before accepting the event

          That's the point: you should NOT accept the event, because else your app is closed. Ignore the event. As sson as you get the response close the application (you will get closeEvent again, so you will need to remember that you already got the response and accept the event).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • jsulmJ jsulm

            @AbhiVarma said in QNetworkAccessManager object not receiving the response when closing application:

            before accepting the event

            That's the point: you should NOT accept the event, because else your app is closed. Ignore the event. As sson as you get the response close the application (you will get closeEvent again, so you will need to remember that you already got the response and accept the event).

            A Offline
            A Offline
            Abhi_Varma
            wrote on last edited by
            #5

            @jsulm before accepting the event i was waiting for 30 seconds to 1 min but still i didnt get finished signal even though server sent me response.

            jsulmJ 1 Reply Last reply
            0
            • A Abhi_Varma

              @jsulm before accepting the event i was waiting for 30 seconds to 1 min but still i didnt get finished signal even though server sent me response.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Abhi_Varma said in QNetworkAccessManager object not receiving the response when closing application:

              i was waiting for 30 seconds to 1 min

              Blocking the event loop? This is not something you should do in an event driven applications. As long as event loop is blocked your slots will not be called.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Abhi_Varma said in QNetworkAccessManager object not receiving the response when closing application:

                i was waiting for 30 seconds to 1 min

                Blocking the event loop? This is not something you should do in an event driven applications. As long as event loop is blocked your slots will not be called.

                A Offline
                A Offline
                AbhiVarma
                wrote on last edited by
                #7

                @jsulm Thanks for the solution. It working now. But i would like to understand why we have to make calls and get all the responses before window closes

                jsulmJ 1 Reply Last reply
                0
                • A AbhiVarma

                  @jsulm Thanks for the solution. It working now. But i would like to understand why we have to make calls and get all the responses before window closes

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @AbhiVarma Well, how should your app receive the response if it is closed? It has to wait for the response...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  A 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @AbhiVarma Well, how should your app receive the response if it is closed? It has to wait for the response...

                    A Offline
                    A Offline
                    AbhiVarma
                    wrote on last edited by
                    #9

                    @jsulm what if i do the following.

                    do
                        {
                        qAppObject->processEvents(QEventLoop::WaitForMoreEvents, 500);
                        }
                        while(m_eApiCallResponseState  != RESPONSE_REQUESTED_STATE);
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • A AbhiVarma

                      @jsulm what if i do the following.

                      do
                          {
                          qAppObject->processEvents(QEventLoop::WaitForMoreEvents, 500);
                          }
                          while(m_eApiCallResponseState  != RESPONSE_REQUESTED_STATE);
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @AbhiVarma Why?
                      Why don't you do it in a clean way as I suggested?
                      But it is up to you to write dirty code which may fail...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @AbhiVarma Why?
                        Why don't you do it in a clean way as I suggested?
                        But it is up to you to write dirty code which may fail...

                        A Offline
                        A Offline
                        AbhiVarma
                        wrote on last edited by AbhiVarma
                        #11

                        @jsulm I am handling it in the closeEvent. I'm waiting in the closeEvent until i get my response. using the following loopk. only once i get response and exiting the loop and accepting the event

                        do
                        {
                        qAppObject->processEvents(QEventLoop::WaitForMoreEvents, 500);
                        }
                        while(m_eApiCallResponseState != RESPONSE_REQUESTED_STATE);

                        jsulmJ 1 Reply Last reply
                        0
                        • A AbhiVarma

                          @jsulm I am handling it in the closeEvent. I'm waiting in the closeEvent until i get my response. using the following loopk. only once i get response and exiting the loop and accepting the event

                          do
                          {
                          qAppObject->processEvents(QEventLoop::WaitForMoreEvents, 500);
                          }
                          while(m_eApiCallResponseState != RESPONSE_REQUESTED_STATE);

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @AbhiVarma I already explained you how you should do it without any dirty workarounds like local event loop...

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2

                          • Login

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