I've made a simple program that bulk-downloads pictures from a web site.
You choose a starting number and an ending number, and the program starts to download all the pictures in between.
I have a TMemo that displays the file name of downloaded files.
When the download starts, Image files are starting to appear in the folder and filenames is added to the TMemo.
But after a few seconds, the program freezes and Windows says that it's "Not Responding". The funny thing is that the program is still running, and continues to download files, as new files continues to appear in the folder.
After a few minutes when the download is complete (Depending on how many images you want to download of corse) The program "Loosens" and is no longer 'Not responding'
There's also a label on the form that is suposed to show the name of the file curently being downloaded, but this does not work.
This is the code that runs when i press the "Download" button:
function GetInetFile (const fileURL, FileName: String): boolean;
const BufferSize = 1024;
var
hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
f: File; sAppName: string;
begin
result := false;
sAppName := ExtractFileName(Application.ExeName) ;
hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
try
hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0) ;
try
AssignFile(f, FileName) ;
Rewrite(f,1) ;
repeat
InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
BlockWrite(f, Buffer, BufferLen)
until BufferLen = 0;
CloseFile(f) ;
if FileSize(lokalfil) < 1500 then begin
DeleteFile(lokalfil);
end else result := True;
finally InternetCloseHandle(hURL)
end
finally InternetCloseHandle(hSession)
end
end;
Procedure finnfil;
begin
if undermapper=TRUE then begin
temp:=IntToStr(nummer);
if nummer < 1000 then mappe:=('/0/') ;
if nummer >= 1000 then begin
if nummer < 10000 then begin
delete (temp,2,3);
mappe:= ('/' +temp +'/');
end;
if nummer >= 10000 then begin
delete (temp,3,3);
mappe:= ('/' +temp +'/');
end;
end;
webfil:= (adresse +mappe +(IntToStr(nummer)) +filtype[s]);
lokalfil:=(IntToStr(nummer) +filtype[s]);
end;
if undermapper=FALSE then begin
webfil:= (adresse +(IntToStr(nummer)) +filtype[s]);
lokalfil:=(IntToStr(nummer) +filtype[s]);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
teljar:=8;
start:=StrToInt(min.Text);
stopp:=StrToInt(maks.Text);
GroupBox1.Enabled:=FALSE;
Button1.Enabled:=FALSE;
min.Enabled:=FALSE;
maks.Enabled:=FALSE;
Memo1.Lines.Add('Starter nedlasting');
Memo1.Lines.Add('av ' +IntToStr(stopp-start+1) +' Bilder');
for nummer:=start to stopp DO begin
teljar:=teljar+1;
for s:=1 to filtyper DO begin
finnfil;
{ showmessage ('Webfil: ' +webfil);
showmessage ('Lokalfil: ' +lokalfil); }
if not FileExists (lokalfil) then begin
Label1.Caption:=('Prøver ' +IntToStr(nummer));
if GetInetFile(webfil, lokalfil) then begin
Memo1.Lines.Add(IntToStr(nummer) +filtype[s] +' OK!');
break;
end;
end;
end;
end;
GroupBox1.Enabled:=TRUE;
Button1.Enabled:=TRUE;
min.Enabled:=TRUE;
maks.Enabled:=TRUE;
Label1.Caption:=('Ferdig!');
ShowMessage ('Ferdig!');
end; As you can see, there's more variables and they are of corse Identified elsewhere
I have wery little knowledge of multi-threading, but i was just thinking that the problem might be that this is a single-threaded aplication? Or am i wrong...?
Is there an easy fix to this freezing problem?
