Forum > General

Killing off stucked thread

<< < (4/4)

balazsszekely:
@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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program kill_fphttpclient; {$mode objfpc}{$H+} {$Define UseCThreads} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes,  SysUtils,  fphttpclient   { you can add units after this }; typeTTestThread = class(TThread)        protected                procedure Execute; override;        public                constructor Create(CreateSuspended: boolean);                var client: TFPHTTPClient;                var response: string;end; constructor TTestThread.Create(CreateSuspended : boolean);begin        inherited Create(CreateSuspended);        Self.client := nil;end; procedure TTestThread.Execute;begin        Self.client := TFPHttpClient.Create(nil);        Client.IOTimeout:= 2000;        Self.response := client.Get('http://185.80.48.255/?check');         client.Free;        client := nil;end; var        thr: TTestThread;        i: integer; begin        thr := TTestThread.Create(false);        i := 0;        repeat                sleep(10);                inc(i);        until (thr.Finished) or (i >= 500);        if (i >= 500) then        begin                writeln('Timeout.');        end        else        begin                writeln('Response: ' + thr.response);        end;        if (not thr.Finished) then        begin                if (thr.client <> nil) then                begin                        thr.client.Terminate;                end;                repeat                        sleep(10);                until thr.Finished;        end;        thr.Free;end.

TCH:
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:
@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?

--- End quote ---
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.

Navigation

[0] Message Index

[*] Previous page

Go to full version