Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The connection does not timeout while using Indy

Tags:

delphi

indy10

I want to download a file from internet and I imagine this should be a simple task. Trying several different approaches I have found that each one has its own drawback. The main issues are:

  • Application freezes until it downloads the file
  • Application freezes forever if the Internet connection is lost/server does not respond.

(details:

How to retrieve a file from Internet via HTTP?
The connection does not timeout while downloading file from internet )

So, finally I used the suggestions I got from several people to use "pro" libraries such as Indy. However, Indy is not much better than the pieces of code I have tried (but it is way much larger and difficult to maintain). While using Indy the application does not freezes only for short periods so it is still (somehow) usable. However, the application cannot be shut down until the download finishes (never if the Internet connections gets broken).

Other people reported the same problem: http://borland.newsgroups.archived.at/public.delphi.internet.winsock/200609/0609079112.html
https://forums.embarcadero.com/thread.jspa?threadID=25199&tstart=90

So, there is some hacking I had to do to TIDAntiFreeze in order to make it work?

Also, the ConnectTimeout property is not recognized.

fIDHTTP := TIDHTTP.Create(NIL);
fIDHTTP.ConnectTimeout:=5000;

Should I drop Indy and return to original idea of downloading the file in a separate thread and end the thread when it does not respond (at least this way I get rid of 3rd party libraries)? There will be unforeseen side effects if I do this?

Using: Delphi 7, Indy 10.1.5 10.5 (probably).

Thanks

like image 288
Server Overflow Avatar asked Jul 06 '10 15:07

Server Overflow


1 Answers

You probably need to use Indy the Indy way: using threads. Indy was specifically designed to work in blocking mode, because that's how most internet protocols work (example: with HTTP, at protocol level, you send a request, then you read the response. You don't send and receive at the same time). TIdAntiFreeze is supposed to help you use some Indy functionality without dealing with threads; I never used that because, at least conceptually, it's an ugly hack.

If you don't want to deal with threads then you should take a look at ICS - it was designed to be used in async mode, without threading. It doesn't need the equivalent of TIdAntiFreeze because it's not blocking. You start a download and you handle some events to get progress and completion notifications. ICS is just as well-known, professional and wildly used as Indy.

like image 99
Cosmin Prund Avatar answered Sep 21 '22 23:09

Cosmin Prund