Recent

Author Topic: TTimer elapsed do not runs asociated event... sometimes  (Read 13520 times)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #15 on: September 18, 2014, 01:34:44 pm »
Fair enough.  Hopefully you can make some progress with reducing this to a reproducible test case so we can all see what's going on.

Good luck.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #16 on: September 18, 2014, 03:20:24 pm »
Because the server thread does not process WM_TIMER, only the GUI thread does.

Edit:
Only the default window procedure calls the callback function for the timer.
« Last Edit: September 18, 2014, 03:22:34 pm by engkin »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #17 on: September 18, 2014, 03:27:20 pm »
Because the server thread does not process WM_TIMER, only the GUI thread does.

Edit:
Only the default window procedure calls the callback function for the timer.
Good catch.  I missed that in your earlier post.   What's the solution then?  Implement his own wndproc and listen for WM_TIMER (via the methods below)?
 
http://lazarus-ccr.sourceforge.net/docs/lcl/controls/twincontrol.wndproc.html
http://wiki.lazarus.freepascal.org/Win32/64_Interface#Processing_non-user_messages_in_your_window
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #18 on: September 18, 2014, 03:37:16 pm »
Because the server thread does not process WM_TIMER, only the GUI thread does.

Edit:
Only the default window procedure calls the callback function for the timer.
Good catch.  I missed that in your earlier post.   What's the solution then?  Implement his own wndproc and listen for WM_TIMER (via the methods below)?
 
http://lazarus-ccr.sourceforge.net/docs/lcl/controls/twincontrol.wndproc.html
http://wiki.lazarus.freepascal.org/Win32/64_Interface#Processing_non-user_messages_in_your_window

I guess if this assumption is correct, which I don't know what to believe because the event sometimes fires and with this assumption it should never fire, then a thread based timer would be a better solution to this problem, since there is no message for the fired event but a direct call, or something along those lines.

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #19 on: September 18, 2014, 03:54:04 pm »
I hate the "remove" link.  :(
----
I agree with Taazz: "a thread based timer".
----
Edit:
I .... never mind.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #20 on: September 18, 2014, 04:02:44 pm »
I hate the "remove" link.  :(
----
I agree with Taazz: "a thread based timer".
----
Edit:
I .... never mind.

Tried to modify your post, deleted it instead?   Been there, done that :)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #21 on: September 18, 2014, 08:27:27 pm »
There are two conditions:
Code: [Select]
procedure TCustomTimer.Timer;
begin
...
  if (FEnabled) and (FInterval > 0) then
    DoOnTimer;
end;

Enable is true and Interval > 0.
Huh, why does Interval have to be > 0? It has always worked just fine with Interval 0 on Delphi at least.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #22 on: September 18, 2014, 08:43:18 pm »
Huh, why does Interval have to be > 0? It has always worked just fine with Interval 0 on Delphi at least.

Really?  How often does it run?

Oh.  According to the online docs our implementation is the same as Delphi
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/ExtCtrls_TTimer_Interval.html

Quote
Note: A 0 value is valid, however the timer will not call an OnTimer event for a value of 0
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Scoops

  • Full Member
  • ***
  • Posts: 100
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #23 on: September 18, 2014, 10:54:12 pm »
I'm probabaly off topic cos its late here. When i last used delphi it is was not advised
to use threads for anything in a gui enviroment, procedures ok but nothing visual.
I say delphi cos it was mentioned earlier. And lazarus tries to be compatible with delphi.
And anything under 50ms doesnt change anything (as far as windows is concerned).
as for linux etc i dont know, but windows its 50ms minimum.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #24 on: September 19, 2014, 12:18:33 am »
I'm probabaly off topic cos its late here. When i last used delphi it is was not advised
to use threads for anything in a gui enviroment, procedures ok but nothing visual.
I say delphi cos it was mentioned earlier. And lazarus tries to be compatible with delphi.

that is still true today on delphi and lazarus.
 
And anything under 50ms doesnt change anything (as far as windows is concerned).
as for linux etc i dont know, but windows its 50ms minimum.

well there was never such a high threshold on winNT based systems ee nt3.1 2000, xp etc I have no recollections for win 95/98 and the rest I'm sorry to say so, but even then it is the CPU that specifies the threshold if we are talking about the same thing it was around 25MS in the win2000 era and it is around 16MS in todays CPUs with the multiple cores and all that.
Care to post any reference?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #25 on: September 19, 2014, 01:07:54 am »
Because the server thread does not process WM_TIMER, only the GUI thread does.

Edit:
Only the default window procedure calls the callback function for the timer.
Good catch.  I missed that in your earlier post.   What's the solution then?  Implement his own wndproc and listen for WM_TIMER (via the methods below)?
 
http://lazarus-ccr.sourceforge.net/docs/lcl/controls/twincontrol.wndproc.html
http://wiki.lazarus.freepascal.org/Win32/64_Interface#Processing_non-user_messages_in_your_window

I guess if this assumption is correct, which I don't know what to believe because the event sometimes fires and with this assumption it should never fire, then a thread based timer would be a better solution to this problem, since there is no message for the fired event but a direct call, or something along those lines.


Because the server thread does not process WM_TIMER, only the GUI thread does.

Edit:
Only the default window procedure calls the callback function for the timer.

The Timer works perfect ALWAYS when i make click in the button. If the server receives a order to run the same procedure like if you click the button, it ALWAYS fails.

I guess engkin is right; there are more timers in the form, i will check all them to see if they all have the same result (as i can remember now, only one timer is modified via code from the server anyway)

Are there any workaround if this is the case? The server must be automatic to process all request.

Also, if a timer is already enabled and a Timer.Enabled:= true is called, the interval is supossed to restart?
« Last Edit: September 19, 2014, 01:15:40 am by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #26 on: September 19, 2014, 04:50:16 am »
Are there any workaround if this is the case? The server must be automatic to process all request.
Use WaitForSingleObject. For a nice example, check the first answer on this stackoverflow link

I came across this Timer that seems to be another possible solution.

Also, if a timer is already enabled and a Timer.Enabled:= true is called, the interval is supossed to restart?
No, according to the source code:
Code: [Select]
procedure TCustomTimer.SetEnabled (value : boolean);
begin
  if (Value <> FEnabled) then
  begin
    FEnabled := value;
    UpdateTimer;
  end;
end;
The new value has to be different to cause any change.

Edit:
Corrected the method. Thanks Taazz.

@Mike.Cornflake, thank you. I fell for this mistake twice, deleting my posts instead of modifying them. This time it did upset me enough to modify a browser extension to remove the Delete Button.
« Last Edit: September 19, 2014, 05:17:55 am by engkin »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #27 on: September 19, 2014, 04:58:32 am »
No, according to the source code:
Code: [Select]
procedure TCustomTimer.SetInterval (value : cardinal);
begin
  if (value <> FInterval) then
  begin
    FInterval := value;
    UpdateTimer;
  end;
end;
The new value has to be different to cause any change.

Correct answer wrong method quoted.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: TTimer elapsed do not runs asociated event... sometimes
« Reply #28 on: September 25, 2014, 04:57:23 am »
Well, thanks to all who show interest on this topic. I was able to solve it with a simple workaround: I just added a new variable which is switched true/false by the client. Depending on this new variable, the asociated event to the Timer is executed or not. Finally, the timer is never disabled. I guess this is the easy solution for situations like this.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

 

TinyPortal © 2005-2018