Recent

Author Topic: Killing off stucked thread  (Read 9118 times)

balazsszekely

  • Guest
Re: Killing off stucked thread
« Reply #15 on: October 24, 2017, 10:24:20 pm »
@TCH

Your example works perfectly on linux, just set Client.IOTimeout to 2000 before connect, you also need  the usual {$Define UseCThreads}. The connect function returns immediately after 2 seconds. Don't forget to open a terminal window to see the result or/and use breakpoints. As for windows you should file a bugreport.

Code: Pascal  [Select][+][-]
  1. program kill_fphttpclient;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. {$Define UseCThreads}
  6.  
  7. uses
  8.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  9.   cthreads,
  10.   {$ENDIF}{$ENDIF}
  11.   Classes,
  12.   SysUtils,
  13.   fphttpclient
  14.  
  15.   { you can add units after this };
  16.  
  17. type
  18. TTestThread = class(TThread)
  19.         protected
  20.                 procedure Execute; override;
  21.         public
  22.                 constructor Create(CreateSuspended: boolean);
  23.                 var client: TFPHTTPClient;
  24.                 var response: string;
  25. end;
  26.  
  27. constructor TTestThread.Create(CreateSuspended : boolean);
  28. begin
  29.         inherited Create(CreateSuspended);
  30.         Self.client := nil;
  31. end;
  32.  
  33. procedure TTestThread.Execute;
  34. begin
  35.         Self.client := TFPHttpClient.Create(nil);
  36.         Client.IOTimeout:= 2000;
  37.         Self.response := client.Get('http://185.80.48.255/?check');
  38.  
  39.         client.Free;
  40.         client := nil;
  41. end;
  42.  
  43. var
  44.         thr: TTestThread;
  45.         i: integer;
  46.  
  47. begin
  48.         thr := TTestThread.Create(false);
  49.         i := 0;
  50.         repeat
  51.                 sleep(10);
  52.                 inc(i);
  53.         until (thr.Finished) or (i >= 500);
  54.         if (i >= 500) then
  55.         begin
  56.                 writeln('Timeout.');
  57.         end
  58.         else
  59.         begin
  60.                 writeln('Response: ' + thr.response);
  61.         end;
  62.         if (not thr.Finished) then
  63.         begin
  64.                 if (thr.client <> nil) then
  65.                 begin
  66.                         thr.client.Terminate;
  67.                 end;
  68.                 repeat
  69.                         sleep(10);
  70.                 until thr.Finished;
  71.         end;
  72.         thr.Free;
  73. end.
« Last Edit: October 24, 2017, 10:28:09 pm by GetMem »

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Killing off stucked thread
« Reply #16 on: October 24, 2017, 11:30:57 pm »
I see, with your changes, it works here too, thanks.

So, let sum it up: if i understood correctly what you and rvk said, FPHTTPClient.IOTimeout is only works if the server is unreachable and FPHTTPClient.Terminate only works if the server is reachable but busy or slow, right?

balazsszekely

  • Guest
Re: Killing off stucked thread
« Reply #17 on: October 25, 2017, 06:11:39 am »
@TCH
Quote
So, let sum it up: if i understood correctly what you and rvk said, FPHTTPClient.IOTimeout is only works if the server is unreachable and FPHTTPClient.Terminate only works if the server is reachable but busy or slow, right?
Yes that's right. If you wonder why are two separate methods basically for the same thing, both IOTimeout and Terminate where contributed to FPHTTPClient by different users at a later time.  IOTimeout is still buggy though in some cases.

 

TinyPortal © 2005-2018