Recent

Author Topic: [solved]disable ttimer ..!  (Read 5395 times)

ali-libre

  • New Member
  • *
  • Posts: 40
[solved]disable ttimer ..!
« on: October 16, 2016, 10:48:27 pm »
I'm create a timer using
Code: Pascal  [Select][+][-]
  1. With ttimer.create(sender :tobject) do
  2. Begin
  3.   Interval := 1100;
  4.   Enable := true;
  5.   Ontimer := @timerdo;
  6. End;

In form create
 Now in the half of program i want to disable Or free it. how can i do that...?
« Last Edit: October 18, 2016, 04:59:19 am by ali-libre »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: disable ttimer ..!
« Reply #1 on: October 16, 2016, 10:55:22 pm »
Now in the half of program i want to disable Or free it. how can i do that...?
Code: [Select]
YourTimerName.Enable := false;
:)

Destroy/free it ? Well, try giving it at least a proper owner so that you might be able to find back your timer in order to reference it.

PS: your code does not compile. Please provide working examples and not some made up fantasy sprouted from your mind.  ;D It is not possible for us to peek into there so we would end up with quite a mess trying to kill the cat  :D
« Last Edit: October 16, 2016, 11:02:02 pm by molly »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: disable ttimer ..!
« Reply #2 on: October 16, 2016, 11:03:33 pm »
First: give the timer a name, e.g. Timer1, or DelayTimer, or whatever. Declare the timer in the private section of the form (if you need it only within the form) or in the public section (if you need it from other forms):

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.   (...)
  4.   private
  5.     Timer1: TTimer;
  6.   (...)

Then: in FormCreate create the timer correctly:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Timer1 := TTimer.Create(self);  // "self" means: the form will ultimately destroy the timer when the program ends
  4.   with Timer1 do begin
  5.     Interval := 1100;
  6.     Enabled := true;
  7.     OnTimer := @timerdo;
  8.   end;

If you later want to disable the timer call
Code: Pascal  [Select][+][-]
  1.   Timer1.Enabled := false;

Normally there is no need to free the timer by yourself because you specified an "owner" in its constructor - this is the class which will free it automatically when it is destroyed.

If you want to free the timer for some reason call Timer1.Free. And set Timer1 to nil to signal that you cannot use it any more. Both calls can be combined with "FreeAndNil()".
Code: Pascal  [Select][+][-]
  1.   Timer1.Free;
  2.   Timer1 := nil;
  3.   // or: FreeAndNil(Timer1);

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: disable ttimer ..!
« Reply #3 on: October 16, 2016, 11:40:24 pm »
It's not really a problem in this particular case but this one just doesn't sit right with me.
Code: Pascal  [Select][+][-]
  1. Enabled := true;
  2. OnTimer := @timerdo;
I would switch those two lines. It's not a problem because you're not multi-threading here (or it's not another component) but I would always first set all the properties correct before setting an enabled (or active) to true.
Code: Pascal  [Select][+][-]
  1. OnTimer := @timerdo;
  2. Enabled := true;

totya

  • Hero Member
  • *****
  • Posts: 720
Re: disable ttimer ..!
« Reply #4 on: October 17, 2016, 12:00:20 am »
I'm create a timer using
Code: Pascal  [Select][+][-]
  1. With ttimer.create(sender :tobject) do
  2. Begin
  3.   Interval := 1100;
  4.   Enable := true;
  5.   Ontimer := @timerdo;
  6. End;

In form create
 Now in the half of program i want to disable Or free it. how can i do that...?

If any created object need manipulate later, do not create this way, assign object to the variable, like as:
Code: Pascal  [Select][+][-]
  1. Timer:=TTimer.Create(Self);

Sample (but see the wp master code too):

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     CreateButton: TButton;
  17.     FreeButton: TButton;
  18.     EnabledButton: TButton;
  19.     procedure CreateButtonClick(Sender: TObject);
  20.     procedure EnabledButtonClick(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.     procedure FreeButtonClick(Sender: TObject);
  23.   private
  24.     Timer: TTimer;
  25.   private
  26.     procedure MyOnTimer(Sender: TObject);
  27.   public
  28.     { public declarations }
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   Timer:= nil;
  43. end;
  44.  
  45. procedure TForm1.CreateButtonClick(Sender: TObject);
  46. begin
  47.   if not(Assigned(Timer))
  48.   then
  49.     begin
  50.       Timer:= TTimer.Create(self);
  51.       ShowMessage('Timer created!');
  52.  
  53.       with Timer do
  54.       begin
  55.         Interval:= 1000;
  56.         OnTimer:= @MyOnTimer;
  57.         Enabled:= true;
  58.       end;
  59.     end
  60.   else
  61.     begin
  62.       Timer.Enabled:= false;
  63.       Showmessage('You need destroy the timer first...');
  64.     end;
  65. end;
  66.  
  67. procedure TForm1.EnabledButtonClick(Sender: TObject);
  68. begin
  69.   if Assigned(Timer)
  70.     then Timer.Enabled:=true
  71.     else Showmessage('You need create the timer first...');
  72. end;
  73.  
  74. procedure TForm1.FreeButtonClick(Sender: TObject);
  75. begin
  76.   if Assigned(Timer)
  77.   then
  78.     begin
  79.       Timer.Enabled:= false;
  80.       FreeAndNil(Timer);
  81.       ShowMessage('Timer destroyed!')
  82.     end
  83.   else Showmessage('You need create the timer first...');
  84. end;
  85.  
  86. procedure TForm1.MyOnTimer(Sender: TObject);
  87. begin
  88.   (Sender as TTimer).Enabled:= false;
  89.  
  90.   ShowMessage('Timer events fired! Timer is now disabled.');
  91. end;
  92.  
  93. end.
« Last Edit: October 17, 2016, 12:02:21 am by totya »

ali-libre

  • New Member
  • *
  • Posts: 40
Re: disable ttimer ..!
« Reply #5 on: October 17, 2016, 03:35:44 am »
So bad. But how lcl or compiler will handle this type of code
It will choosen random name for it?
Now, how i must create some number of class...?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: disable ttimer ..!
« Reply #6 on: October 17, 2016, 03:51:00 am »
So bad. But how lcl or compiler will handle this type of code
It will choosen random name for it?
The name of the component will be empty.

Quote
Now, how i must create some number of class...?
Simply by creating them. You have several options for that. one of the easiest:

Code: Pascal  [Select][+][-]
  1. var
  2.   TimerArray: array[0..9] of TTimer;
  3.  
  4. procedure TForm1.InitTimers;
  5. var
  6.   i: integer;
  7. begin
  8.   for i := low(TimerArray) to High(TimerArray) do
  9.   begin
  10.     TimerArray[i] := TTimer.Create(nil);
  11.  
  12.     with TimerArray[i] do
  13.     begin
  14.       TimerArray[i].interval ;= 1100;
  15.       Ontimer := @timerdo;
  16.       Enabled := true
  17.     end;
  18.   end;
  19. end;
  20.  
  21. procedure TForm1.FreeTimers;
  22. var
  23.   i: integer;
  24. begin
  25.   for i := low(TimerArray) to High(TimerArray)
  26.    do TimerArray[i].Free;
  27. end;
  28.  

edit: example fixed as spotted by totya
« Last Edit: October 17, 2016, 07:53:08 am by molly »

totya

  • Hero Member
  • *****
  • Posts: 720
Re: disable ttimer ..!
« Reply #7 on: October 17, 2016, 06:13:46 am »
begin needed after do...

totya

  • Hero Member
  • *****
  • Posts: 720
Re: disable ttimer ..!
« Reply #8 on: October 17, 2016, 07:02:36 am »
So bad. But how lcl or compiler will handle this type of code
It will choosen random name for it?
Now, how i must create some number of class...?

For example, and with "name" :)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Spin, ExtCtrls, contnrs;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ButtonCreate: TButton;
  17.     FreeButton: TButton;
  18.     Memo1: TMemo;
  19.     procedure ButtonCreateClick(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormDestroy(Sender: TObject);
  22.     procedure FreeButtonClick(Sender: TObject);
  23.   private
  24.     TimerComponentList: TComponentList;
  25.   private
  26.     procedure MyOnTimer(Sender: TObject);
  27.     { private declarations }
  28.   public
  29.     { public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.FormCreate(Sender: TObject);
  42. begin
  43.   TimerComponentList:= nil;
  44. end;
  45.  
  46. procedure TForm1.MyOnTimer(Sender: TObject);
  47. begin
  48.   with (Sender as TTimer) do
  49.   begin
  50.     Enabled:= false;
  51.  
  52.     Memo1.Lines.Add('OnTimer event, Name is: "'+Name+'". Timer is now disabled.');
  53.   end;
  54. end;
  55.  
  56. procedure TForm1.ButtonCreateClick(Sender: TObject);
  57. var
  58.   i: integer;
  59. begin
  60.   if not(Assigned(TimerComponentList)) then
  61.   begin
  62.     Memo1.Clear;
  63.  
  64.     TimerComponentList:= TComponentList.Create(true);
  65.  
  66.     for i:=0 to 10 do
  67.     begin
  68.       TimerComponentList.Add(TTimer.Create(nil));
  69.  
  70.       with TimerComponentList.Last as TTimer do
  71.       begin
  72.         Name:= 'Timer'+IntToStr(i);
  73.  
  74.         Interval:= 1000;
  75.         OnTimer:= @MyOnTimer;
  76.         Enabled:= true;
  77.       end;
  78.     end;
  79.   end;
  80. end;
  81.  
  82. procedure TForm1.FreeButtonClick(Sender: TObject);
  83. begin
  84.   if Assigned(TimerComponentList)
  85.     then FreeAndNil(TimerComponentList);
  86. end;
  87.  
  88. procedure TForm1.FormDestroy(Sender: TObject);
  89. begin
  90.   FreeButton.Click;
  91. end;
  92.  
  93.  
  94. end.
  95.  

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: disable ttimer ..!
« Reply #9 on: October 17, 2016, 07:54:00 am »
begin needed after do...
Yups, my bad. thank you for spotting that. I fixed my example.

ali-libre

  • New Member
  • *
  • Posts: 40
Re: disable ttimer ..!
« Reply #10 on: October 18, 2016, 04:58:39 am »
thank's guys...
molly's example is more understandable for me

 

TinyPortal © 2005-2018