Timer event handler reentrancy lazarus / 31-LCL ---------------------------------------------------------------------- MarkMLl · 2020-02-26 16:56 Are timer event handlers protected from reentry, or is it good practice to disable the timer at the head of a handler if there is even a slight risk that it might overrun the timer interval? In the current case I'm using gtk2 on a KDE desktop, but from my POV if there were /any/ widget set that didn't implicitly protect against reentry it would be worth doing it explicitly. MarkMLl ---------------------------------------------------------------------- lucamar · 2020-02-26 17:22 Quote from: MarkMLl on February 26, 2020, 05:56:36 pmAre timer event handlers protected from reentry, or is it good practice to disable the timer at the head of a handler if there is even a slight risk that it might overrun the timer interval? IIRC (and I might not), they are not protected; but even if they were, stopping them on entry to the handler would still be good practice, and most of all for very short-interval timers. Of course, if your timer fires once an hour (and the handler doesn't interact with the user!), stopping it might a little excessive ---------------------------------------------------------------------- MarkMLl · 2020-02-26 17:38 Thanks for that. Unless anybody can say that they are /definitely/ disabled (on all widget sets and OSes, and that this will never change), I agree that it's better to err on the side of caution. MarkMLl ---------------------------------------------------------------------- lucamar · 2020-02-26 17:46 Quote from: MarkMLl on February 26, 2020, 06:38:45 pmI agree that it's better to err on the side of caution. Indeed! My code is always choked full with tests for things that "can never happen" and "will never happen". We call those tests "Murphy- guards" ---------------------------------------------------------------------- winni · 2020-02-26 18:04 Hi! I use this code to prevent problems: 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 implementationvar timerbusy : boolean= false;...procedure TForm1.Timer1Timer(Sender:TObject);beginif timerbusy then exit;timerbusy := true;..........timerbusy := false;end;    Winni ---------------------------------------------------------------------- MarkMLl · 2020-02-26 18:31 If you're doing that you should be protecting your  if ... := true;  with a critical section, or using InterlockedIncrement(). OK, so I agree that it's the main (GUI) thread and should only be revelling in the attentions of a single thread, but if protecting against the possibility that things go wrong it's worth doing things properly. MarkMLl ---------------------------------------------------------------------- lucamar · 2020-02-26 20:30 Winni, if you're worried to that point, wouldn't it be better (and more logical) to do what we're talking about, i.e.: 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 procedure TForm1.Timer1Timer(Sender:TObject);begin  (Sender as TTimer).Enabled := False;  try    {do your thing!}  finally    (Sender as TTimer).Enabled := True;  end;end; I mean, you already have a boolean "variable" there, with the advantage that simply setting it to False will make the Timer stop firing the event! ---------------------------------------------------------------------- ASerge · 2020-02-26 20:47 Quote from: MarkMLl on February 26, 2020, 05:56:36 pmAre timer event handlers protected from reentry, or is it good practice to disable the timer at the head of a handler if there is even a slight risk that it might overrun the timer interval? Since GUI event handlers are handled by a single thread, re-entry protection is only needed if the code inside the event calls waiting functions for other events that recursively call the current handler. ---------------------------------------------------------------------- winni · 2020-02-26 20:59 Quote from: ASerge on February 26, 2020, 09:47:58 pmSince GUI event handlers are handled by a single thread, re-entry protection is only needed if the code inside the event calls waiting functions for other events that recursively call the current handler. I searched that in this clarity in the docs - in vain. That sentence should be put in the docs. Thanx Winni ---------------------------------------------------------------------- MarkMLl · 2020-02-26 21:16 Quote from: winni on February 26, 2020, 09:59:14 pmQuote from: ASerge on February 26, 2020, 09:47:58 pmSince GUI event handlers are handled by a single thread, re-entry protection is only needed if the code inside the event calls waiting functions for other events that recursively call the current handler. I searched that in this clarity in the docs - in vain. That sentence should be put in the docs. Thanx Winni Meaning no disrespect to anybody, but provided that it's accurate as far as all widget sets etc. are concerned. I remember catching PostMessage() reentering a few months ago, so I'm inclined towards caution. The highly erratic bug that I've been keeping an eye open for over the last few days- rather than actively hunting- can only be caused (a) by a timer reentering or (b) by a shim invoked by Synchronize() having a similar effect. Both of them call a UDP broadcast at random intervals, into which I've inserted an explicit pacing delay if multiple broadcasts go to different subnets (because an obfuscated timestamp is in the message): and the delay is substantially longer than the timer interval. A particular smoking gun is that I think I've seen form activity which can only be caused by the timer firing at the same time as an error dialog(ue) is on the screen... I'm not quite sure what to make of that one yet. MarkMLl