Recent

Author Topic: [SOLVED] TFPTimer.Stop  (Read 5178 times)

aradeonas

  • Hero Member
  • *****
  • Posts: 824
[SOLVED] TFPTimer.Stop
« on: October 29, 2014, 10:22:58 pm »
Hi,
For first time I want to test TFPTimer but when I want to disable timer, application will be freeze.
Can any one test this code and tell me why?
Code: [Select]
  var
  Timer:TFPTimer; 

procedure TForm1.Button4Click(Sender: TObject);
begin
 timer:=TFPTimer.Create(self);
 timer.OnTimer:=@TimerTick;
 Timer.Interval:=100;
 timer.StartTimer;
end;

procedure TForm1.TimerTick(Sender: TObject);
begin
self.Caption:='Finished';
Timer.StopTimer;
end; 
« Last Edit: October 30, 2014, 01:26:44 pm by aradeonas »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: TFPTimer.Stop
« Reply #1 on: October 30, 2014, 01:04:14 am »
Calling StopTimer inside the thread of the timer (read: its OnTimer) is like asking the thread to wait for itself to finish (which will never happen)

1-Never call StopTime inside OnTimer.
2-Call Terminate instead.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: TFPTimer.Stop
« Reply #2 on: October 30, 2014, 07:45:09 am »
Thank you,
1- How can I AutoDisable timer after 1 tick?
2- How can I terminate a TFPTimer?(I have not access to its driver terminate)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TFPTimer.Stop
« Reply #3 on: October 30, 2014, 09:01:43 am »
1- How can I AutoDisable timer after 1 tick?
Set Enabled to false in the OnTimer.
2- How can I terminate a TFPTimer?(I have not access to its driver terminate)
Set Enabled to false anytime you want to terminate it.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: TFPTimer.Stop
« Reply #4 on: October 30, 2014, 10:08:05 am »
@Leledumbo please read my question and engkin answer.
With TFPTimer application will be freeze if I change enabled in OnTimer.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TFPTimer.Stop
« Reply #5 on: October 30, 2014, 12:00:22 pm »
IMO you will have to create your own driver or maybe your own unit.

I copy - pasted fpTimer to a new unit MyfpTimer with following modifications:
Code: [Select]
TFPTimerThread = class(TThread)
  private
    FTimerDriver: TFPTimerDriver;
    Function Timer : TFPCustomTimer;
  public
    counter: Integer;  //<-- new field
    procedure Execute; override;
    constructor CreateTimerThread(ATimerDriver: TFPTimerDriver);
  end;
and
Code: [Select]
procedure TFPTimerThread.Execute;
...
    If Assigned(T) and not terminated then
        begin  //this
        Synchronize(@T.Timer);
        inc(counter);  //this
        if counter=2 then begin  //<--  here are hardcoded 2 but you can define public property Count
          T.FEnabled:=False; //this
          Terminate;  //this
          end;  //this
        end;  //this

With this modification (added 8 lines) it triggers two timer events, no mem. leaks, no freezing.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: TFPTimer.Stop
« Reply #6 on: October 30, 2014, 01:26:19 pm »
Thank you Blaazen.

 

TinyPortal © 2005-2018