Recent

Author Topic: TTimer stops after wake from sleep state  (Read 7795 times)

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #30 on: September 18, 2019, 08:46:35 am »
@karloromic

Made a little demo program with the routine from the wiki page.
Runs well with Linux and should also do with Windows.

The timer interval is set to 1 second because the reactions are very quick.
Test it!

Zip file  attached.

Winni
Wow, thanks, i will test it out now  :)

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #31 on: September 18, 2019, 09:32:10 am »
@winni

Code: Pascal  [Select][+][-]
  1. unit NetAlive1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. // Winni Bartnick, 18.9.2019
  6.  
  7.  
  8. interface
  9.  
  10. uses
  11.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  12.  
  13. type
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Timer1: TTimer;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure GetMyIPAddress;
  24.     procedure Timer1Timer(Sender: TObject);
  25.   private
  26.  
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. uses
  37.   fphttpclient, RegexPr;
  38.  
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.Button1Click(Sender: TObject);
  45. begin
  46.   Close;
  47. end;
  48.  
  49.  
  50. // stolen fom https://wiki.lazarus.freepascal.org/fphttpclient
  51. // modified
  52. procedure TForm1.GetMyIPAddress;
  53. var
  54.   HTTPClient: TFPHTTPClient;
  55.   IPRegex: TRegExpr;
  56.   RawData: string;
  57.   msg: string;
  58. begin
  59.   try
  60.     HTTPClient := TFPHTTPClient.Create(nil);
  61.     IPRegex := TRegExpr.Create;
  62.     try
  63.       //returns something like:
  64.       {
  65. <html><head><title>Current IP Check</title></head><body>Current IP Address: 44.151.191.44</body></html>
  66.       }
  67.       RawData := HTTPClient.Get('http://checkip.dyndns.org');
  68.       // adjust for expected output; we just capture the first IP address now:
  69.       IPRegex.Expression := RegExprString('\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b');
  70.       //or
  71.       //\b(?:\d{1,3}\.){3}\d{1,3}\b
  72.       if IPRegex.Exec(RawData) then
  73.       begin
  74.         msg := IPRegex.Match[0];
  75.       end
  76.       else
  77.       begin
  78.         msg := 'Got invalid results getting external IP address.';
  79.         // Details:'+LineEnding+ RawData;
  80.       end;
  81.     except
  82.       on E: Exception do
  83.       begin
  84.         msg := 'Error retrieving external IP address '; //+E.Message;
  85.       end;
  86.     end;
  87.     Label1.Caption := msg;
  88.   finally
  89.     HTTPClient.Free;
  90.     IPRegex.Free;
  91.   end;
  92. end;
  93.  
  94. procedure TForm1.Timer1Timer(Sender: TObject);
  95. begin
  96.   Timer1.Enabled := False;
  97.   GetMyIPAddress;
  98.   Label2.Caption := TimeToStr(Time);
  99.   Timer1.Enabled := True;
  100. end;
  101.  
  102. end.
  103.  

Tested it out, also added Timer1.Enabled so that timer doesnt fire every second but after function finishes.
It is fast, but not any faster than Ping request or HTTPMethod("HEAD" request (when there is a connection). And yes, there is no problem when there is connection and when device is online but when there is no connection or when router loses connection to ISP for example (i simulate every possible situation) one request can hang for 30 seconds which is a long time.
Is there a timeout that actually works for HTTPClient because i tried everything i found in documentation and internet but no property affects that hang time when there is no connection.
Thats my real problem i cant figure out how to solve, even if i put it in thread, thread would still need to finish that job (wait for 30 secs) before it will terminate (the clean way).
« Last Edit: September 18, 2019, 10:02:00 am by karloromic »

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TTimer stops after wake from sleep state
« Reply #32 on: September 18, 2019, 10:44:16 am »
Is there a timeout that actually works for HTTPClient because i tried everything i found in documentation and internet but no property affects that hang time when there is no connection.way).
You tried everything. But we don't know what you tried so you might want to say what you tried so we don't give duplicate solutions.

Did you try setting the THTTPClient.ConnectTimeout ?
https://forum.lazarus.freepascal.org/index.php/topic,41428.msg288313.html#msg288313
(Fixed in 3.1.1 so I don't know what FPC version you are using)

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #33 on: September 18, 2019, 10:57:27 am »
Is there a timeout that actually works for HTTPClient because i tried everything i found in documentation and internet but no property affects that hang time when there is no connection.way).
You tried everything. But we don't know what you tried so you might want to say what you tried so we don't give duplicate solutions.

Did you try setting the THTTPClient.ConnectTimeout ?
https://forum.lazarus.freepascal.org/index.php/topic,41428.msg288313.html#msg288313
(Fixed in 3.1.1 so I don't know what FPC version you are using)
Code: Pascal  [Select][+][-]
  1.   HTTP := THTTPSend.Create;
  2.  
  3.   //I tried:
  4.   HTTP.Timeout := 2000;
  5.   HTTP.Sock.ConnectionTimeout := 2000;
  6.  
  7.   HTTP.HTTPMethod('HEAD', check_head[I]);
  8.  

But it still hangs 15-30 secs
Tried THTTPClient.ConnectTimeout with THTTPClient - the same thing
« Last Edit: September 18, 2019, 11:00:42 am by karloromic »

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TTimer stops after wake from sleep state
« Reply #34 on: September 18, 2019, 11:01:50 am »
But it still hangs 15-30 secs
Tried THTTPClient.ConnectTimeout with THTTPClient - the same thing
What version of FPC are you using?

if you use fpc trunk, today the issue 33745 TInetSocket connect timeout is resolved, so you can use the following code  : <snip>

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #35 on: September 18, 2019, 11:02:46 am »
3.0.4
I meant TFPHTTPClient not THTTPClient*
« Last Edit: September 18, 2019, 11:04:51 am by karloromic »

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TTimer stops after wake from sleep state
« Reply #36 on: September 18, 2019, 11:04:43 am »
3.0.4
According to the bugtracker it should be fixed in 3.2.0 (or 3.1.1 trunk).

So no other way to use a faster timeout.

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #37 on: September 18, 2019, 11:11:05 am »
3.0.4
According to the bugtracker it should be fixed in 3.2.0 (or 3.1.1 trunk).

So no other way to use a faster timeout.
So what can i do now?

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TTimer stops after wake from sleep state
« Reply #38 on: September 18, 2019, 11:18:51 am »
I'm not sure. You could apply the fixes yourself but recompiling Lazarus won't do anything with FPC itself. And this is a part of FPC. Recompiling FPC isn't easy to do with a standard installation.

You could try fpcupdeluxe to install new lazarus with stable fixes.

Otherwise you'll need another method to see if you're online.
(Or use another library like Synapse)

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #39 on: September 18, 2019, 11:27:11 am »
Code: Pascal  [Select][+][-]
  1. uses laz_synapse
  2.  
  3. var HTTP: THTTPSend;
  4.  
  5. HTTP := THTTPSend.Create;
  6.  
  7. HTTP.Timeout := 2000;
  8. HTTP.Sock.ConnectionTimeout := 2000;  
  9.  
  10. HTTP.HTTPMethod('HEAD', 'http://www.google.com'); //or https with fpopenssl, openssl units
  11.  
isnt this synapse?
Its the same thing.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TTimer stops after wake from sleep state
« Reply #40 on: September 18, 2019, 11:38:44 am »
Code: Pascal  [Select][+][-]
  1. uses laz_synapse
  2.  
  3. var HTTP: THTTPSend;
  4.  
  5. HTTP := THTTPSend.Create;
  6.  
  7. HTTP.Timeout := 2000;
  8. HTTP.Sock.ConnectionTimeout := 2000;  
  9.  
  10. HTTP.HTTPMethod('HEAD', 'http://www.google.com'); //or https with fpopenssl, openssl units
  11.  
isnt this synapse?
Its the same thing.
It's very different.
TFPHTTPClient is from FPC and THTTPSend is from Synapse.

And the synapse-method hangs for you if you do it like that?

Because this works fine for me (gives me a 0 result after 3 seconds):

Code: Pascal  [Select][+][-]
  1. uses laz_synapse, httpsend;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   HTTP: THTTPSend;
  6. begin
  7.   HTTP := THTTPSend.Create;
  8.   HTTP.Timeout := 3000;
  9.   HTTP.Sock.ConnectionTimeout := 3000;
  10.   HTTP.HTTPMethod('HEAD', 'http://httpstat.us/200?sleep=50000');
  11.   Showmessage(HTTP.ResultCode.ToString);
  12. end;

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #41 on: September 18, 2019, 12:07:17 pm »
It's very different.
TFPHTTPClient is from FPC and THTTPSend is from Synapse.

And the synapse-method hangs for you if you do it like that?

Because this works fine for me (gives me a 0 result after 3 seconds):

Code: Pascal  [Select][+][-]
  1. uses laz_synapse, httpsend;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   HTTP: THTTPSend;
  6. begin
  7.   HTTP := THTTPSend.Create;
  8.   HTTP.Timeout := 3000;
  9.   HTTP.Sock.ConnectionTimeout := 3000;
  10.   HTTP.HTTPMethod('HEAD', 'http://httpstat.us/200?sleep=50000');
  11.   Showmessage(HTTP.ResultCode.ToString);
  12. end;

It doesnt give 0 result after 3 seconds but after 5 and that is the bottom limit, what if i want to check two addresses (one can be blocked or down at the moment), for each request i would then wait 5000 ms, so if check 3 urls i would wait minimum total of 15 seconds.

Try my modified project bellow to see it in action.
https://ufile.io/8ctvhvrs

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: TTimer stops after wake from sleep state
« Reply #42 on: September 18, 2019, 12:12:58 pm »
It doesnt give 0 result after 3 seconds but after 5 and that is the bottom limit, what if i want to check two addresses (one can be blocked or down at the moment), for each request i would then wait 5000 ms, so if check 3 urls i would wait minimum total of 15 seconds.
That's not the timeout problem. That's your TTimer that's at 5000. So if you start the program, it waits until the timer hits, which is 5 seconds and then it has the timeout of 1000ms (1 second). So the first showmessage you get after 6 seconds.

Why are you still working with a TTimer. We already established a TThread would be much better.

Anyway... try to remove the timer and put a button on the form with a call to this procedure and press the button. You'll see the real timeout is what you set it to.

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #43 on: September 18, 2019, 12:17:58 pm »
It doesnt give 0 result after 3 seconds but after 5 and that is the bottom limit, what if i want to check two addresses (one can be blocked or down at the moment), for each request i would then wait 5000 ms, so if check 3 urls i would wait minimum total of 15 seconds.
That's not the timeout problem. That's your TTimer that's at 5000. So if you start the program, it waits until the timer hits, which is 5 seconds and then it has the timeout of 1000ms (1 second). So the first showmessage you get after 6 seconds.

Why are you still working with a TTimer. We already established a TThread would be much better.

Anyway... try to remove the timer and put a button on the form with a call to this procedure and press the button. You'll see the real timeout is what you set it to.
I used TTimer only for demo purpose, not in real program. I use while no terminated as you suggested
That solved my problem!  :D thank youuuuuuu
« Last Edit: September 18, 2019, 12:20:52 pm by karloromic »

 

TinyPortal © 2005-2018