Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-09

TFPTimer does not fire, why?

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

Ì have a problem using TFpTimer on Linux (FPC 3.0.4, Lazarus 2.0.8 on Raspbian).

The application is a small GUI test app where I have added a timer in order to update the display with incoming data.
So I have it set up like this:

{$mode Delphi}
interface
uses
...
fptimer,
...
type

  TfrmMain = class(TForm)
  ...
  private
    FTimCheckInputs: TFpTimer;
  ...
    procedure OnTimCheckInputs(Sender: TObject);
  ...
implementation
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  ...
  FTimCheckInputs := TFpTimer.Create(NIL);
  FTimCheckInputs.OnTimer := OnTimCheckInputs;
  FTimCheckInputs.Interval := 500; // 500 ms latency
  ...
end;
procedure TfrmMain.OnTimCheckInputs(Sender: TObject);
begin
  ShowInByte; //<== This is NEVER reached!
end;
procedure TfrmMain.btnOpenComClick(Sender: TObject);
begin
  ...
  if FRemote.OpenPort(edComPort.Text, StrToInt(cbxBaud.Text)) then
  begin
    ...
    FTimCheckInputs.Enabled := true;
  end
  else
  begin
    ...
    FTimCheckInputs.Enabled := false;
  end;
end;
procedure TfrmMain.FormClose(Sender: TObject; var CloseAction:
TCloseAction);
begin
  FTimCheckInputs.Enabled := false;
  FTimCheckInputs.Free;
  ...
end;

When I click the btnOpenCom button I get to the button event and the code reachers the line:

  FTimCheckInputs.Enabled := true;

So presumably here the timer should start and fire after 500 ms as set up in the FormCreate procedure.
But it never does! I do not get into have put a
breakpoint there but it never fires, whereas other breakpoints do fire so I can see that for example btnOpenComClick fires off properly and the timer should be enabled.

And before you ask, I *have* this at the beginning of the lpr file: uses

  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}

Why does the FpTimer event not fire?
I am assuming the Interval property is in milliseconds...

When I step through the code and reach the place where the timer is enabled the text that appears when I hover over the Enabled keyword is very strange:

"FTimCheckInputs.Enabled = TYPE FPTIMER has no component named ENABLED.
published property TFPTimer.Enabled:Boolean"

Is it not possible to start the timer using Enabled?????

Bo Berglund Developer in Sweden

Reply by email

Michael Van Canneyt
Bo Berglund wrote 82 lines
Ì have a problem using TFpTimer on Linux (FPC 3.0.4, Lazarus 2.0.8 on Raspbian). The application is a small GUI test app where I have added a timer in order to update the display with incoming data. So I have it set up like this: {$mode Delphi} interface uses ... fptimer, ... type TfrmMain = class(TForm) ... private FTimCheckInputs: TFpTimer; ... procedure OnTimCheckInputs(Sender: TObject); ... implementation procedure TfrmMain.FormCreate(Sender: TObject); begin ... FTimCheckInputs := TFpTimer.Create(NIL); FTimCheckInputs.OnTimer := OnTimCheckInputs; FTimCheckInputs.Interval := 500; // 500 ms latency ... end; procedure TfrmMain.OnTimCheckInputs(Sender: TObject); begin ShowInByte; //<== This is NEVER reached! end; procedure TfrmMain.btnOpenComClick(Sender: TObject); begin ... if FRemote.OpenPort(edComPort.Text, StrToInt(cbxBaud.Text)) then begin ... FTimCheckInputs.Enabled := true; end else begin ... FTimCheckInputs.Enabled := false; end; end; procedure TfrmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin FTimCheckInputs.Enabled := false; FTimCheckInputs.Free; ... end; When I click the btnOpenCom button I get to the button event and the code reachers the line: FTimCheckInputs.Enabled := true; So presumably here the timer should start and fire after 500 ms as set up in the FormCreate procedure. But it never does! I do not get into have put a breakpoint there but it never fires, whereas other breakpoints do fire so I can see that for example btnOpenComClick fires off properly and the timer should be enabled. And before you ask, I *have* this at the beginning of the lpr file: uses {$IFDEF UNIX} cthreads, {$ENDIF} Why does the FpTimer event not fire?

Do you call checksynchronize at regular intervals ? The default timer is threaded, and the OnTimer event is called in the main thread. For this to work, you need to call checksynchronize at regular intervals in your main thread.

Michael.

Reply by email

Bo Berglund
to Michael Van Canneyt

On Mon, 7 Sep 2020 12:40:07 +0200 (CEST), Michael Van Canneyt via fpc-pascal

Show quoted text 6 lines
> Why does the FpTimer event not fire? Do you call checksynchronize at regular intervals ? The default timer is threaded, and the OnTimer event is called in the main thread. For this to work, you need to call checksynchronize at regular intervals in your main thread.

Before I read your reply I tested another thing:
I dropped a regular timer from the component palette onto the form intending to use that instead of FpTimer.

So I made an event function by doubleclicking the event as usual and added the same call there.
I set the new timer to disabled by default so I can enable it on successful port open like the FpTimer.

Once all that was done I again stepped through the application with breakponts in both event functions and amazingly the FpTimer event now fired! And after it was done the new timer also called its event...

In fact it now seems to work where it did not before.

So next I *removed* the newly dropped timer and its references in the code and now the FpTimer still works. How very strange!

Concerning calling CheckSynchronize I do not really know where it can be done...
I think you do not mean that I should put a timer on the main form and then in its OnTimer event call CheckSynchronize?
This program is a GUI application and I do not know where one could put a call like that.

Bo Berglund Developer in Sweden

Reply by email

Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 14 lines
On Mon, 7 Sep 2020 12:40:07 +0200 (CEST), Michael Van Canneyt via fpc-pascal wrote: >> Why does the FpTimer event not fire? > > Do you call checksynchronize at regular intervals ? The default timer is > threaded, and the OnTimer event is called in the main thread. For this to > work, you need to call checksynchronize at regular intervals in your main > thread. Before I read your reply I tested another thing: I dropped a regular timer from the component palette onto the form intending to use that instead of FpTimer.

That is because you then add the LCL to the application, and in their application object, they call checksynchronize on idle.

Even if you remove the LCL timer, the checksynchronize still will do it's job.

Michael.

Reply by email