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. The problem between HTTP and QEvetnLoop
Forum Updated to NodeBB v4.3 + New Features

The problem between HTTP and QEvetnLoop

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 259 Views 2 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.
  • J Offline
    J Offline
    jack reemoon
    wrote on last edited by
    #1

    QNetworkAccessManager* m_httpManager = new QNetworkAccessManager();
    QDateTime m_LastReemoonDeviceErrData = QDateTime::currentDateTime();

    while (true)
    {
            int reemoonDeviceErrDataTimeCount = m_LastReemoonDeviceErrData.secsTo(QDateTime::currentDateTime());
        if(reemoonDeviceErrDataTimeCount >=3){
            m_LastReemoonDeviceErrData = QDateTime::currentDateTime();
            QString url = "http://192.168.10.29:8899/Api/Customer/GetCustomerDeviceInfo";
            QString postData = "";
            QNetworkRequest request;
            request.setUrl(QUrl(url));
            request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
            QMap<QString, QString> headers;
            QMapIterator<QString, QString> iterator(headers);
            while (iterator.hasNext()) {
                iterator.next();
                request.setRawHeader(iterator.key().toUtf8(),iterator.value().toUtf8());
            }
            QNetworkReply* reply = m_httpManager->post(request,postData.toUtf8());
            /*
            QEventLoop eventLoop;
            connect(reply, &QNetworkReply::finished, [&](){
                QString result = reply->readAll();
                //qDebug()<<QDateTime::currentDateTime()<<reply->errorString()<<"----"<<result;
                reply->deleteLater();
                eventLoop.quit();
            });
            eventLoop.exec();*/
            //添加事件循环机制,返回后再运行后面的
    
            QEventLoop eventLoop;
            connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
            eventLoop.exec();
            QString result = reply->readAll();
            reply->deleteLater();
    
        }
    
        //QThread::msleep(3000);
    }
    if(m_httpManager)
    {
        m_httpManager->deleteLater();
    }
    

    If using GDB to run this program, there will be a thread constantly creating and exiting. What is the reason for this?

    [New Thread 0x7fffc5f50700 (LWP 100506)]
    [Thread 0x7fffc5f50700 (LWP 100506) exited]
    [New Thread 0x7fffc5f50700 (LWP 100511)]
    [Thread 0x7fffc5f50700 (LWP 100511) exited]

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are creating event loops in a loop, that's probably the reason for all these threads.

      It is much better to not wait in loop like this. Use signals and slots to respond to replies as they come. You can send a new request in the slot answering to QNetworkReply::finished, for example: this will behave the same but without waiting, without an infinite loop and without custom QEventLoops.

      (Z(:^

      J 1 Reply Last reply
      2
      • sierdzioS sierdzio

        You are creating event loops in a loop, that's probably the reason for all these threads.

        It is much better to not wait in loop like this. Use signals and slots to respond to replies as they come. You can send a new request in the slot answering to QNetworkReply::finished, for example: this will behave the same but without waiting, without an infinite loop and without custom QEventLoops.

        J Offline
        J Offline
        jack reemoon
        wrote on last edited by
        #3

        @sierdzio yes,I know, but I have to wait for the result returned by HTTP before proceeding to the next step. If I use signals and slots, I won't be able to synchronize the results. What should I do?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I don't see why you won't be able to synchronize the results. Handle the results when you get a finished signal. This will involve splitting your current code into smaller functions, but that is only good for the code in general.

          (Z(:^

          J 1 Reply Last reply
          0
          • sierdzioS sierdzio

            I don't see why you won't be able to synchronize the results. Handle the results when you get a finished signal. This will involve splitting your current code into smaller functions, but that is only good for the code in general.

            J Offline
            J Offline
            jack reemoon
            wrote on last edited by
            #5

            @sierdzio Because this is a loop, I have to wait for the corresponding return value every time. If I use signals and slots, will it cause the first return value to respond only when the second request is made?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              I'm saying you should break the loop. Remove it completely.

              The loop can be replaced completely by signals and slots:

              1. Entry point, you set up your initial conditions.
              2. Execute HTTP request.
              3. In slot, you read the reply and decide if another request is needed. If yes, then you call method 2. again.
              4. If no new request is needed, you don't call method 2. but instead do the next steps you want to do now that you have your complete information downloaded from HTTP server.

              I realise this might be sometimes a bit hard to fit into existing application logic, but it is a simple and easy system, will be easy to maintain.

              (Z(:^

              J 1 Reply Last reply
              3
              • sierdzioS sierdzio

                I'm saying you should break the loop. Remove it completely.

                The loop can be replaced completely by signals and slots:

                1. Entry point, you set up your initial conditions.
                2. Execute HTTP request.
                3. In slot, you read the reply and decide if another request is needed. If yes, then you call method 2. again.
                4. If no new request is needed, you don't call method 2. but instead do the next steps you want to do now that you have your complete information downloaded from HTTP server.

                I realise this might be sometimes a bit hard to fit into existing application logic, but it is a simple and easy system, will be easy to maintain.

                J Offline
                J Offline
                jack reemoon
                wrote on last edited by
                #7

                @sierdzio ok,I try it,thank you

                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