Timer with multiple different OnTimer functions - weird behavior lazarus / 21-General ---------------------------------------------------------------------- rwebb616 · 2021-05-31 17:10 I'm trying to use a single timer for multiple purposes by redefining the procedure attached to the OnTimer event. So one procedure does a 3 second countdown and then displays "GO!" as such: 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 TfScoreboard.CountDownClockTimer(Sender: Tobject);begin  if ClockInterval >= 1 then    begin      countdown.Caption:=ClockInterval.Tostring;      CenterCountdown;      PlayCountDown;      dec(ClockInterval);    end  else    begin      GameClock.Enabled:=false;      countdown.Caption := 'GO!';      CenterCountdown;      if options.sounds[4] <> '' then PlayMediaFromFile(options.sounds[4]);      delay(1000);    end;end; And the other is a countdown clock with a variable duration set at runtime on the control form: 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 TfScoreboard.GameClockTimer(Sender: Tobject);begin  if options.timer.interval <= 0 then begin    // Handle time expired    countdown.Caption := 'TIME''S UP!';    countdown.font.Size := 150;    countdown.font.name := 'Segoe UI Black';    centercountdown;    PlayWAVFromFile(options.sounds[7]);    GameClock.Enabled:=false;    exit;  end;  if options.timer.interval <= options.timer.WarningPoint then begin    // Handle playing warning tick-tock  end;  countdown.Caption := GetTimerString(options.Timer.Interval);  CenterCountdown;  dec(options.timer.interval);end; The OnTimer event is changed depending on what procedure is using the timer by assigning it with OnTimer:=@procedure; So what is happening is if I run either one individually they run as they should, but if I run one following the other it's almost as if both procedures are running .. the second one counts down by 2s and if I run it multiple times it gets faster so not sure.  I would attach the code but even without the sounds and stuff it's 14MB.  If I need to post more of the code I can. Rich ---------------------------------------------------------------------- Handoko · 2021-05-31 17:17 Please provide a compile-able source code so we can test and help you fix the issue. This kind of problem is relatively hard to solve without inspecting the whole source code. To make the source code smaller, you can remove the unnecessary things (form, unit, audio, images, etc) but keeping the code that can show the issue. Also don't forget to remove backup and lib folders and the binary file. ---------------------------------------------------------------------- rwebb616 · 2021-05-31 17:20 Ok got it small enough but it won't have any resource files to play when running it - which I think is okay.  Probably at a minimum have to define a title slide and a scoreboard slide. Also it's designed for multiple monitors so you'd have to have a second monitor for it to work right. ---------------------------------------------------------------------- Handoko · 2021-05-31 17:22 Sorry cannot help. I'm a Linux user. ---------------------------------------------------------------------- rwebb616 · 2021-05-31 17:23 Ah Yes, it's a windows only program - could probably rewrite it for Linux but that would be an undertaking Requires the use of SDL2 library for sounds and I'm using a couple windows Fonts.. other than that I think it would work. Rich ---------------------------------------------------------------------- MarkMLl · 2021-05-31 20:40 Not having tried it but I'd observe that if anything in your handlers calls Application.ProcessMessages things are likely to go reentrant and you're thoroughly screwed. I suggest disabling the timer on entry to the handler and re-enabling on exit, and instead of changing the handler use the timer's tag as a hint telling common code which handler should be run. MarkMLl ---------------------------------------------------------------------- rwebb616 · 2021-05-31 20:42 That's a good idea.. I always forget about the tag property.  I just rewrote it to use a boolean to tell the single ontimer procedure what section it was supposed to run... seems to work.