Results 1 to 5 of 5

Thread: asynchronous vs. synchroneous

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default asynchronous vs. synchroneous

    I'm trying to check if websites repond to me. So, I have a list and I'm sequentially testing them with QNetworkAccessManager like shown below. The problem is that although I'm trying, the process may still be a bit asyncroneous, meaning that the replyFinished() function is not called without a lag or too late or while the next URL is already tested. I know this because I sometimes get wrong results. Does anyone have ideas, I'd be very thankful.

    This is my main function:
    Qt Code:
    1. QUrl url; QNetworkAccessManager manager;
    2.  
    3. for(int y=1; y<100+1; y++){
    4.  
    5. tT.setSingleShot(true);
    6. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    7. connect(&manager, SIGNAL(finished(QNetworkReply*)), &q, SLOT(quit()));
    8.  
    9. QNetworkReply *reply = manager.get(QNetworkRequest(url));//this URL is different in every for cycle
    10.  
    11. tT.start(5000); // 5s timeout
    12. q.exec();
    13.  
    14. if(tT.isActive()){
    15. // download complete
    16. tT.stop();
    17. replyFinished(reply);
    18.  
    19. } else {
    20. // timeout
    21. }
    22. QApplication::processEvents();
    23. }
    24.  
    25. QApplication::processEvents();
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    This is my replyFinished() slot:
    Qt Code:
    1. void MyClass::replyFinished(QNetworkReply* reply)
    2. {
    3.  
    4. QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    5.  
    6. if (statusCodeV.toInt()!=0){
    7. //report that website reponded
    8. }
    9. else{
    10. //report that website didn't respond
    11. }
    12.  
    13. delete reply;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Thnaks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: asynchronous vs. synchroneous

    Read this article: [wiki]Keeping the GUI Responsive[/wiki].
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    timmu (28th August 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: asynchronous vs. synchroneous

    Thanks Wysota, this is exactly the article I'm following but still I'm seeing this asynchroneous behaviour. There must be something wrong with what I'm adding to this code in the article, something I just cannot figure out. I've been trying for a long time.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: asynchronous vs. synchroneous

    A... sorry, I didn't notice the event loop in your code. Increase the timeout. Those 5 seconds in the article were just an example. You can even completely get rid of the timeout if you're certain QNetworkAccessManager will report a failure when it doesn't manage to contact the website.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    timmu (28th August 2009)

  7. #5
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: asynchronous vs. synchroneous

    Increasing the timeout is a great suggestion and I have tried that one too but nothing much has changed. My problem isn't that the program has a timeout problem. The replyFinished() function sometimes returns the wrong verdict. It sometimes says the website is OK when it is not and the other way around.

    Is increasing the timpout the best way to make the program more syncronous?
    Last edited by timmu; 28th August 2009 at 10:56.

Similar Threads

  1. Replies: 10
    Last Post: 6th May 2011, 16:00
  2. QSocket asynchronous read-write
    By ct in forum Newbie
    Replies: 1
    Last Post: 21st March 2006, 09:59
  3. Asynchronous server msg vs synchronous functions
    By nouknouk in forum Qt Programming
    Replies: 9
    Last Post: 2nd February 2006, 17:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.