[SOLVED ]Catching TFPHTTPClient timeout and avoid freeze lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- torbente · 2018-05-30 15:00 I use this simple function to read data from webpages Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Read_URL_GET(url:string):string;beginwith TFPHTTPClient.Create(nil) do  try    Read_URL_GET:= Get(url);  except    on E:Exception do       begin       Read_URL_GET:= 'ERROR';       end;  end;end; It returns 'ERROR' when something go wrong, but if the page do not responds, it freeze the app meanwhile. Are there any simple way to avoid this situations? ---------------------------------------------------------------------- Leledumbo · 2018-05-31 03:47 Quote from: torbente on May 30, 2018, 05:00:30 pmIt returns 'ERROR' when something go wrong, but if the page do not responds, it freeze the app meanwhile. Are there any simple way to avoid this situations? Wrap in a thread. ---------------------------------------------------------------------- balazsszekely · 2018-05-31 04:33 I agree with Leledumbo, without a worker thread this issue is unsolvable. More useful info: http://forum.lazarus.freepascal.org/index.php/topic,38472.0.html ---------------------------------------------------------------------- torbente · 2018-05-31 13:57 ok, y make it work with threads (timer1.interval := 3000 here) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  numero : integer;  procedure readfromweb();beginform1.memo1.lines.add(read_ur l_get('https://myweb.com/sleep.php')); // this pages delays 10 seconds to answerform1.timer1.Enabled:=false;end; procedure TForm1.Button1Click(Sender: TObject);begintimer1.Enabled:=true;numero := BeginThread(TThreadFunc(@readfromweb));end; procedure TForm1.Timer1Timer(Sender: TObject);beginmemo1.lines.add('Server is down or busy. Try again later.');KillThread(numero);timer1.Enabled:=false;end; Can this be sintetized in one function?? Somehting like... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsServerOnline():boolean;begin// all the previous code here... IsServerOnline := true; // Or falseend; ---------------------------------------------------------------------- af0815 · 2018-05-31 14:03 I think it is not a good idea to use visual components like memo inside of threads. See http://wiki.freepascal.org/Multithreaded_Application_Tutorial and https://forum.lazarus.freepascal.org/index.php/topic,38851.msg265181.h tml#msg265181 ... and alot of other posts ... ---------------------------------------------------------------------- torbente · 2018-05-31 14:11 The memo is only for testing. I only need know if the server is responding in less than 3 second. I had read a lot of posts, and im still searching for new ones. Quote from: af0815 on May 31, 2018, 04:03:52 pmI think it is not a good idea to use visual components like memo inside of threads. See http://wiki.freepascal.org/Multithreaded_Application_Tutorial and https://forum.lazarus.freepascal.org/index.php/topic,38851.msg265181.h tml#msg265181 ... and alot of other posts ... ---------------------------------------------------------------------- balazsszekely · 2018-05-31 14:31 @torbente QuoteCan this be sintetized in one function?? Somehting like...Not in a function, no. But in a class yes. I already gave you an example how to do a http requests(download file, parse webpage to a string, post files, etc...) in a worker thread. Take a look at the attachment in this post(uhttprequest.pas): http://f orum.lazarus.freepascal.org/index.php/topic,41310.msg286833.html#msg28 6833 You have to modify it a little bit, rewrite the Execute method, set fphttpclient.timeout and you're good to go. Also take into account this post: http://forum.lazarus.freepascal.org/index.php/topic,38472.m sg261333.html#msg261333 Basically everything is implemented, but I'm not gonna spend another half our writing a test project ---------------------------------------------------------------------- torbente · 2018-06-01 07:59 @getmem First, i really apreciated your time and help.  I read and run your code, and i do not understand many things (i had never worked with classes, for example). But for what i understood from your test project, it is the same thing i posted in this topic, but with another aproach. Anyway, i will use it to study and try to learn using reverse engineering  What i can do with my (very) limited lazarus/fpc knowledge looks easy to do, but not in an "elegant" (or easy) way. I was thinking in a possible solution, but due to the very hard job it minds for my entire project, this is the basic idea(i will create a backup of my project first anyway)(off topic: it is time to add a simple "Rename Project" option to the lazarus GUI) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var // GlobalURLtoRead : string;StringFromURL : string;ThreadNumber:Integer; procedure Read_URL_GET();beginwith TFPHTTPClient.Create(nil) do  try    StringFromURL := Get(URLtoRead);  except    on E:Exception do       begin       StringFromURL := 'ERROR';       end;  end;end; procedure GetDataFromServer(URL:String;TimeOUTQuery:integer);beginURLtoRead  := URL;StringFromURL  := 'DOWN': // set by defaultThreadNumber:= BeginThr ead(TThreadFunc(@Read_URL_GET));WaitForThreadTerminate(ThreadNumber,Ti meOUTQuery);end; EDITED: This is working perfectly (until now)!! I even dont need a timer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GetDataFromServer('https://myweb.com/',1500); // this page returns 'ok'if StringFromURL  = 'DOWN' then // what to do if server is downelse if StringFromURL = 'ERROR' then // if the server gives a bad answerelse // and here what to do if everything go fine ; I dont know if this code could be improved, but do the job. ---------------------------------------------------------------------- d.ioannidis · 2018-06-09 19:30 Hi,   if you use fpc trunk, today the issue 33745 TInetSocket connect timeout is resolved, so you can use the following code  : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     function Read_URL_GET(url:string):string;    begin    with TFPHTTPClient.Create(nil) do      try        ConnectTimeout := 2000;  <-- Add this line.        Read_URL_GET:= Get(url);      except      on E:Exception do           begin           Read_URL_GET:= 'ERROR';         end;      end;    end;   if the connection can't be established it raises a seConnectTimeOut exception.  regards,