Synapse : Stop download after X secondes lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Mukatai · 2014-03-04 09:09 Hello, In my project i need to stop a download after X secondes. I use this code for download the file in Memorystream : Code: [Select]function DownloadFile(sURL,sLogin,sPass:string;var OutStr:TMemoryStream) : boolean  ; var   HTTPclient: THTTPSend; begin   result := false ;   HTTPclient := THTTPSend.Create ;   HTTPclient.AddPortNumberToHost := true ;   HTTPclient.UserName := sLogin ;   HTTPclient.Password := sPass ;   if HTTPclient.HTTPMethod('GET',sURL) then     begin       showmessage('Fonction start') ;       if HTTPclient.ResultCode = 200 then         begin           HTTPclient.Document.SaveToStream(OutStr);           result := true ;         end;     end ;   HTTPclient.Free; end;    How can I stop the download after X seconds? Thanks, @+ ---------------------------------------------------------------------- Leledumbo · 2014-03-04 09:45 Use TTimer, set the number of milliseconds in the Interval property. Fill in OnTimer with the download stopper code. On the other hand, the normal code can set Enabled to true upon starting download and to false when it's done. ---------------------------------------------------------------------- Mukatai · 2014-03-04 10:34 thank you for your very fast reply ! I can't use a TTimer because it's an application console. I use this fonction in web server (FCL) with Threaded:=True. When the client calls a specific address i download a file from an other HTTP server and send it. But the user can define the address of second http server and if he enter an address of flux the download continuous until memory is full. This is why I want to stop the download after X seconds... Do you see any other possibility ? Ideally not create another function for stop the download... Thanks ---------------------------------------------------------------------- Cyrax · 2014-03-04 13:08 You may try something like this: Pseudocode: Code: [Select]Loop.    Read N bytes.   Check if we have downloaded more than X seconds (GetTickCount etc.) or we have dowloaded whole file.   If we are, abort loop else continue loop.   Write N bytes. End loop. ---------------------------------------------------------------------- Mukatai · 2014-03-04 14:34 This is what I think but I do not know how? ---------------------------------------------------------------------- Leledumbo · 2014-03-04 15:08 QuoteI can't use a TTimer because it's an application consolehttp://www.freepascal.org/docs-html/fcl/fptimer/index.html ---------------------------------------------------------------------- Fred vS · 2014-03-04 19:58 Quotehttp://www.freepascal.org/docs-html/fcl/fptimer/index.html Oops, did not know  @ Leledumbo : many thanks. ---------------------------------------------------------------------- Mukatai · 2014-03-04 20:40 Quote from: Leledumbo on March 04, 2014, 04:08:07 pmQuoteI can't use a TTimer because it's an application consolehttp://www.freepascal.org/docs-html/fcl/fptimer/index.html I did not know this timer, thank you ! After much thought, I was inspired by examples with progress bar. I created an obejct of HTTPsend with a time variable and a callback procedure. In this callback i compare the download start date and abort the HTTPsend procedure when time is up... Code: [Select]THTTPDownload = class(THTTPSend)   public     ExpireTime : TDateTime ;     constructor create ;     procedure HTTPCallback(Sender: TObject; Reason: THookSocketReason; const Value: string);   end; //CODE..... procedure THTTPDownload.HTTPCallback(Sender: TObject; Reason: THookSocketReason; const Value: string); var   DTnow : TDateTime ; begin   DTnow := Now ;   if ExpireTime < DTnow then     begin       Abort;       exit ;     end; end; Constructor THTTPDownload.create ; begin   inherited Create ;   ExpireTime := now + (1.0 / 1440) ; end;  function DownloadFile(sURL,sLogin,sPass:string;var OutStr:TMemoryStream) : boolean  ; var   HTTPclient : THTTPDownload ; begin   result := false ;   HTTPclient := THTTPDownload.Create ;   HTTPclient.AddPortNumberToHost := true ;   HTTPclient.UserName := sLogin ;   HTTPclient.Password := sPass ;   HTTPclient.Sock.OnStatus := @HTTPclient.HTTPCallback ;   if HTTPclient.HTTPMethod('GET',sURL) then     begin         if HTTPclient.ResultCode = 200 then         begin           HTTPclient.Document.SaveToStream(OutStr);           result := true ;         end;     end ;   HTTPclient.Free; end;  I have not fully tested, but the few tests performed are good... What do you think? Thanks for the support of all member! @+