Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file from the Internet while being able to abort the download any time

I'd like to download a file from my Delphi program in a separate thread dedicated to the download.

The problem is that the main program can be closed any time (thus the download thread can be terminated any time as well). So even if there is no connection or the server lags for precious seconds I need a way to terminate the download thread within a second or two.

What function do you guys recommend to use?

I've experimented with InterOpenURL/InternetReadFile but it doesn't have a time-out parameter. It does have an asynchronous version but I could't find any working Delphi examples and I'm not sure if the asynchronous version will protect me from hangs...

Using sockets have been recommended to me before, but TClientSocket doesn't seem to have a time-out function either.

I need to be fully-prepared so if the user has Internet-connection problems on his/her computer or the webserver keeps lagging my application doesn't hang before closing.

Please keep in mind when answering that I don't want to use neither any third party components nor Indy. Any example code is greatly appreciated.

Thank you!

like image 779
Steve Avatar asked Aug 23 '10 14:08

Steve


2 Answers

This works.

var
  DownloadAbort: boolean;

procedure Download(const URL, FileName: string);
var
  hInet: HINTERNET;
  hURL: HINTERNET;
  Buffer: array[0..1023] of AnsiChar;
  BufferLen, amt: cardinal;
  f: file;
const
  UserAgent = 'Delphi Application'; // Change to whatever you wish
begin
  DownloadAbort := false;
  hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  try
    hURL := InternetOpenUrl(hInet, PChar(URL), nil, 0, 0, 0);
    try
      FileMode := fmOpenWrite;
      AssignFile(f, FileName);
      try
        Rewrite(f, 1);
        repeat
          InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
          BlockWrite(f, Buffer[0], BufferLen, amt);
          if DownloadAbort then
            Exit;
        until BufferLen = 0;
      finally
        CloseFile(f);
      end;
    finally
      InternetCloseHandle(hURL);
    end;
  finally
    InternetCloseHandle(hInet);
  end;
end;

In practice, the code above will be part of your thread's Execute method, and you do not need DownloadAbort; intead, you check

if Terminated then
  Exit;
like image 122
Andreas Rejbrand Avatar answered Oct 11 '22 10:10

Andreas Rejbrand


TWinSocketStream has a timeout. You basically create a blocking socket, and then create a TWinSocketStream using that socket for reading/writing. (sort of like a using a StreamWriter). Then you loop until the connection is closed, the thread is terminated, or you reach the number of expected bytes. There's a WaitForData() which allows you to check for incoming data w/ a timeout, so you're never 'locked' past that timeout value.

You would, though, have to handle the http process yourself. ie, send the 'GET', and then read the response header to determine how many bytes to expect, but thats not too difficult.

like image 25
GrandmasterB Avatar answered Oct 11 '22 12:10

GrandmasterB