Recent

Author Topic: Timer problem: unneeded OnTimer call  (Read 2153 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2707
    • UVviewsoft
Timer problem: unneeded OnTimer call
« on: September 01, 2022, 09:29:39 pm »
I test this on Linux x64 (gtk2 and qt5).
Demo attached.

What app must do. When I start typing (e.g. I hold 'd') in TEdit, timer starts, and on each TEdit.OnChange timer is disabled+enabled. I expect to see OnTimer call only when I release the pressed key.

But: I see OnTimer call also when I press the key. It is unneeded for me.

I want to receive the OnTimer in buffered way - if I type faster than Timer1.Interval I want to get only the LAST OnTimer. Not all intermediate OnTimer calls. Demo supports this - it does in TEdit.OnChange

Timer1.Enabled:= false;
Timer1.Enabled:= true;


Lazarus 2.3.0 (rev main-2_3-1386-g23b2324f9f) FPC 3.2.3 x86_64-linux-gtk2
« Last Edit: September 01, 2022, 09:47:15 pm by AlexTP »

rvk

  • Hero Member
  • *****
  • Posts: 7014
Re: Timer problem: unneeded OnTimer call
« Reply #1 on: September 01, 2022, 09:36:04 pm »
I want OnTimer call only on releasing the key.
Why not put this in TEdit.OnKeyUp in that case?

AlexTP

  • Hero Member
  • *****
  • Posts: 2707
    • UVviewsoft
Re: Timer problem: unneeded OnTimer call
« Reply #2 on: September 01, 2022, 09:38:48 pm »
Because the final app will use OnCaretPosChanged in ATSynEdit, while in demo I made the test for OnChange, was easier to demonstate the problem.

rvk

  • Hero Member
  • *****
  • Posts: 7014
Re: Timer problem: unneeded OnTimer call
« Reply #3 on: September 01, 2022, 09:43:06 pm »
Because the final app will use OnCaretPosChanged in ATSynEdit, while in demo I made the test for OnChange, was easier to demonstate the problem.
Then you need to explain what you want.

If you want the timer to activate when releasing a key, you should use the event for releasing the key (i.e. OnKeyUp). The OnChange is triggered on every change (so holding the key will trigger it multiple times).

AlexTP

  • Hero Member
  • *****
  • Posts: 2707
    • UVviewsoft
Re: Timer problem: unneeded OnTimer call
« Reply #4 on: September 01, 2022, 09:46:06 pm »
I want to receive the OnTimer in buffered way - if I type faster than Timer.Interval I want to get only the LAST OnTimer. Not all intermediate OnTimer calls. Demo supports this - it does in TEdit.OnChange

Timer1.Enabled:= false;
Timer1.Enabled:= true;

rvk

  • Hero Member
  • *****
  • Posts: 7014
Re: Timer problem: unneeded OnTimer call
« Reply #5 on: September 01, 2022, 09:53:31 pm »
I still don't understand.

Do you want the timer event on the keydown, keyup, onchange or something else?

You can also enable the timer in the OnChange and disable the timer in the TTimer.OnTimer event.
In that case you will only get one timer event.

But you need to look at when you want to enable it again.

What is the idea behind the timer?


AlexTP

  • Hero Member
  • *****
  • Posts: 2707
    • UVviewsoft
Re: Timer problem: unneeded OnTimer call
« Reply #6 on: September 01, 2022, 10:06:09 pm »
The idea is - fire the app API event after "caret pos is changed" (TEdit.OnChange is not the same - it was only to show the problem). but not immediately, only after a pause!

rvk

  • Hero Member
  • *****
  • Posts: 7014
Re: Timer problem: unneeded OnTimer call
« Reply #7 on: September 01, 2022, 10:12:37 pm »
So enable it on an event of your choice (only when it's disabled) and disable it in the timer event.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Timer problem: unneeded OnTimer call
« Reply #8 on: September 01, 2022, 10:45:43 pm »
The idea is - fire the app API event after "caret pos is changed" (TEdit.OnChange is not the same - it was only to show the problem). but not immediately, only after a pause!
Using the OnChange event is a good place to activate your TTimer and to deactivate simple do it inside the TTimer event itself, else you have a logical problem.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

AlexTP

  • Hero Member
  • *****
  • Posts: 2707
    • UVviewsoft
Re: Timer problem: unneeded OnTimer call
« Reply #9 on: September 01, 2022, 10:53:13 pm »
Quote from: KodeZwerg
Using the OnChange event is a good place to activate your TTimer and to deactivate simple do it inside the TTimer event itself, else you have a logical problem.
Demo already does it! Problem is not here.

rvk

  • Hero Member
  • *****
  • Posts: 7014
Re: Timer problem: unneeded OnTimer call
« Reply #10 on: September 01, 2022, 11:02:55 pm »
Quote from: KodeZwerg
Using the OnChange event is a good place to activate your TTimer and to deactivate simple do it inside the TTimer event itself, else you have a logical problem.
Demo already does it! Problem is not here.
You can't use OnChange for enabling the timer without firing it multiple times.
If you really want to do that you need to make sure not to enable it again.

For example.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   if Timer1.Tag = 0 then
  4.   begin
  5.     Timer1.Interval:= 400;
  6.     Timer1.Enabled:= true;
  7.     Timer1.Tag := 1;
  8.   end
  9. end;

Then the timer only triggers once.
But when do you want to trigger it again?
(You'll need to set the Tag to 0 then)

AlexTP

  • Hero Member
  • *****
  • Posts: 2707
    • UVviewsoft
Re: Timer problem: unneeded OnTimer call
« Reply #11 on: September 01, 2022, 11:28:51 pm »
no no no. I need to fire Timer once __per each block of editings__, ie __per one holding of letter-key__. So I hold 'd' for 3 seconds, and I need to fire Timer once for these 3 seconds. But problem- it's fired TWICE.

rvk

  • Hero Member
  • *****
  • Posts: 7014
Re: Timer problem: unneeded OnTimer call
« Reply #12 on: September 01, 2022, 11:55:36 pm »
no no no. I need to fire Timer once __per each block of editings__, ie __per one holding of letter-key__. So I hold 'd' for 3 seconds, and I need to fire Timer once for these 3 seconds. But problem- it's fired TWICE.
That's what the OnKeyUp is for.
It's only fired once when the key is released.

With the tag method in OnKeyDown the timer should only fire once and you can reset tag to 0 in the OnKeyUp event.
« Last Edit: September 02, 2022, 12:53:13 am by rvk »

AlexTP

  • Hero Member
  • *****
  • Posts: 2707
    • UVviewsoft
Re: Timer problem: unneeded OnTimer call
« Reply #13 on: September 02, 2022, 07:14:10 am »
OnKeyUp will fix it in this demo, but it will not fix timer issue in my map app. For the main app, I use this timer for different event (OnChangeCaretPos).

rvk

  • Hero Member
  • *****
  • Posts: 7014
Re: Timer problem: unneeded OnTimer call
« Reply #14 on: September 02, 2022, 08:03:18 am »
OnKeyUp will fix it in this demo, but it will not fix timer issue in my map app. For the main app, I use this timer for different event (OnChangeCaretPos).
And why not use OnKeyUp if it fixes the problem you described?

Otherwise you need to show the real problem with the actual component.
« Last Edit: September 02, 2022, 08:07:40 am by rvk »

 

TinyPortal © 2005-2018