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. Why QNetworkReply do not finished immediately sometimes while network is not connected

Why QNetworkReply do not finished immediately sometimes while network is not connected

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 322 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.
  • HansonH Offline
    HansonH Offline
    Hanson
    wrote on last edited by
    #1

    I have code like this:

    bool canConnectToInternet()
    {
    		QNetworkAccessManager netManager;
    		QNetworkRequest m_request(Host);
    
    		QNetworkReply* reply = netManager.get(m_request);
    
    		QTimer timer;
    		timer.setInterval(3000);
    		timer.setSingleShot(true);
    		QEventLoop loop;
    		QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
    		QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    		timer.start();
    		loop.exec();
    		if (timer.isActive())
    		{  // not time out
    			timer.stop();
    			if (reply->error() == reply->NoError)
    			{
    				return true;
    			}
    			qDebug() << "canConnectToInternet: " << reply->error();
    		}
    		else
    		{
    			qDebug() << "canConnectToInternet: timeout";
    			QObject::disconnect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    		}
    		return false;
    }
    

    My question is that when the net is connected it work well, and i cut the net when running the program, and it will cost 3 ms then print "canConnectToInternet: timeout".But when I restart the app it immediately return.

    Thanks!

    “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
    —— Martin Golding

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @Hanson said in Why QNetworkReply do not finished immediately sometimes while network is not connected:

      cost 3 ms

      Ok, define the difference between "immediately" and 3ms.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      J.HilkJ HansonH 2 Replies Last reply
      1
      • VRoninV VRonin

        @Hanson said in Why QNetworkReply do not finished immediately sometimes while network is not connected:

        cost 3 ms

        Ok, define the difference between "immediately" and 3ms.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @VRonin thats your concern? And not the content of that function? 🙈


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • VRoninV VRonin

          @Hanson said in Why QNetworkReply do not finished immediately sometimes while network is not connected:

          cost 3 ms

          Ok, define the difference between "immediately" and 3ms.

          HansonH Offline
          HansonH Offline
          Hanson
          wrote on last edited by
          #4

          @VRonin I wrote it wrong.--! It cost 3 seconds.

          “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
          —— Martin Golding

          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