PDA

View Full Version : asynchronous vs. synchroneous



timmu
28th August 2009, 09:55
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:


QUrl url; QNetworkAccessManager manager;
QEventLoop q; QTimer tT;

for(int y=1; y<100+1; y++){

tT.setSingleShot(true);
connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
connect(&manager, SIGNAL(finished(QNetworkReply*)), &q, SLOT(quit()));

QNetworkReply *reply = manager.get(QNetworkRequest(url));//this URL is different in every for cycle

tT.start(5000); // 5s timeout
q.exec();

if(tT.isActive()){
// download complete
tT.stop();
replyFinished(reply);

} else {
// timeout
}
QApplication::processEvents();
}

QApplication::processEvents();

}


This is my replyFinished() slot:


void MyClass::replyFinished(QNetworkReply* reply)
{

QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );

if (statusCodeV.toInt()!=0){
//report that website reponded
}
else{
//report that website didn't respond
}

delete reply;
}


Thnaks!

wysota
28th August 2009, 09:57
Read this article: Keeping the GUI Responsive.

timmu
28th August 2009, 10:00
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.

wysota
28th August 2009, 10:08
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.

timmu
28th August 2009, 10:48
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?