Recent

Author Topic: Using events in a console, non-gui, application ...  (Read 467 times)

xint

  • New Member
  • *
  • Posts: 33
Using events in a console, non-gui, application ...
« on: February 18, 2025, 12:16:43 pm »

Hi,
I have a console application of a database server where I would like to use a timer and react to its event. What can be done with a very few clicks in LCL applications turns out to be unsolvable for my level of knowledge - despite days of research. It is certainly not unsolvable, but my knowledge is the limiting factor.

A short example would be really great. If it is not self-explanatory, I would be very happy to receive a few lines of text as well.

Thanks, Stefan

---

I don't think that it matters, but the OS is Windows 7+, x64.


dbannon

  • Hero Member
  • *****
  • Posts: 3297
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Using events in a console, non-gui, application ...
« Reply #1 on: February 18, 2025, 12:41:03 pm »
 
Here is a bit of copy and paste with some changes, untested and only to give you ideas ...

Code: Pascal  [Select][+][-]
  1. var
  2.     LocalTimer : TTimer = nil;
  3.  
  4. procedure SomeProc();
  5. begin
  6.     ....
  7.     LocalTimer := TTimer.Create(Nil);
  8.     LocalTimer.OnTimer:= @AfterSometime;    // thats the procedure called when timer 'fires'.
  9.     LocalTimer.Interval:=500;
  10.     LocalTimer.Enabled := True;
  11. end;
  12.  
  13. procedure AfterSometime();
  14. begin
  15.     LocalTimer.Enabled := False;             // Don't want to hear from you again
  16.     DoSomething();
  17. end;  
  18.  
  19. procedure CleanupProc;    // maybe destroy ??  Important !!
  20. begin
  21.     if LocalTimer = Nil then exit();
  22.     LocalTimer.Free;
  23.     LocalTimer := nil;
  24. end;  
     
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

xint

  • New Member
  • *
  • Posts: 33
Re: Using events in a console, non-gui, application ...
« Reply #2 on: February 18, 2025, 01:27:59 pm »
Thank you very much for your fast response and the clean code.

I will give that a try for sure, but I'm sure that I tried
Code: Pascal  [Select][+][-]
  1. .OnTimer:= @AfterSometime;
already, with no success.

I use the same approach when I create a popup-menu on-the-fly in LCL applications. But from my memories, this approach did not work in my console application.
But as I wrote, I'll try that for sure ASAP !


Nimbus

  • New Member
  • *
  • Posts: 20
Re: Using events in a console, non-gui, application ...
« Reply #3 on: February 18, 2025, 02:00:18 pm »
You may want to have a look at the TFPTimer class
https://www.freepascal.org/docs-html/fcl/fptimer/tfptimer.html

There's a complete example in the repo
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl-base/examples/testtimer.pp

Using events in the console apps can be a little bit tricky, cause you don't get an message loop from LCL. So you have to do some extra work (like calling that CheckSynchronize method periodically, as in the example) to make sure the events are processed.

Bart

  • Hero Member
  • *****
  • Posts: 5538
    • Bart en Mariska's Webstek
Re: Using events in a console, non-gui, application ...
« Reply #4 on: February 18, 2025, 02:18:40 pm »
I will give that a try for sure, but I'm sure that I tried
Code: Pascal  [Select][+][-]
  1. .OnTimer:= @AfterSometime;
already, with no success.

The AfterSometime procedure must be a method (procedure of object) of a class, not a "plain" procedure.

Bart

xint

  • New Member
  • *
  • Posts: 33
Re: Using events in a console, non-gui, application ...
« Reply #5 on: February 18, 2025, 02:26:57 pm »
Quote
The AfterSometime procedure must be a method (procedure of object) of a class, not a "plain" procedure.

Bart

I appreciate your clarification. If I'm not asking for too much ... a few lines of code would be very nice.

xint

  • New Member
  • *
  • Posts: 33
Re: Using events in a console, non-gui, application ...
« Reply #6 on: February 18, 2025, 02:36:56 pm »
You may want to have a look at the TFPTimer class
https://www.freepascal.org/docs-html/fcl/fptimer/tfptimer.html

Great suggestion and thanks a million for the link !

TRon

  • Hero Member
  • *****
  • Posts: 4154
Re: Using events in a console, non-gui, application ...
« Reply #7 on: February 18, 2025, 03:49:18 pm »
I appreciate your clarification. If I'm not asking for too much ... a few lines of code would be very nice.

Even though I'm not Bart, there you go
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses
  4.   {$ifdef linux}
  5.   cthreads,
  6.   {$endif}
  7.   sysutils, fptimer;
  8.  
  9. type
  10.   TEventHandlers = object
  11.     procedure TimerEvent(Sender: TObject);
  12.   end;
  13.  
  14. var
  15.   Count  : integer = 0;
  16.  
  17. procedure TEventHandlers.TimerEvent(Sender: TObject);
  18. begin
  19.   writeln('timer event fired');
  20.   inc(count);
  21. end;
  22.  
  23.  
  24. var
  25.   Events : TEventHandlers;
  26.   Timer  : TFPTimer;
  27.  
  28.  
  29. begin
  30.   Timer                := TFPTimer.Create(nil);
  31.   Timer.Enabled        := false;
  32.   Timer.UseTimerThread := true;
  33.   Timer.Interval       := 1000;
  34.   Timer.OnTimer        := @Events.TimerEvent;
  35.   Timer.Enabled        := true;
  36.  
  37.   writeln('waiting... ');
  38.   while count < 5 do
  39.   begin
  40.     sleep(1);
  41.   end;
  42.  
  43.   Timer.Free;
  44. end.
  45.  

Happy coding !
Today is tomorrow's yesterday.

Bart

  • Hero Member
  • *****
  • Posts: 5538
    • Bart en Mariska's Webstek
Re: Using events in a console, non-gui, application ...
« Reply #8 on: February 18, 2025, 03:53:45 pm »
I appreciate your clarification. If I'm not asking for too much ... a few lines of code would be very nice.

Untested code.
Code: Pascal  [Select][+][-]
  1. type
  2.   TTimerHandler = class
  3.     procedure AfterSometime(Sender: TObject);
  4.   end;
  5.  
  6. procedure TTimerHandler.AfterSomeTime(Sender: TObject);
  7. begin
  8.   writeln('I am the timer handler, I can do whatever you want me to ...');
  9. end;
  10.  
  11. var
  12.   Timer: TTimer;
  13.   TimerHandler: TTimerHandler;
  14.  
  15. begin
  16.   Timer := TTimer.Create(nil);
  17.   TimerHandler := TTimerHandler.Create;
  18.   try
  19.     Timer.OnTimer := @TimerHandler.AfterSomeTime;
  20.     Timer.Interval := 1000;
  21.     Timer.Enabled := True;
  22. ...
  23.  
  24.   finally
  25.     Timer.Free;
  26.     TimeHandler.Free;
  27.   end;//try..finally
  28. end.

Bart

[ETA] TRon beat me to it.

TRon

  • Hero Member
  • *****
  • Posts: 4154
Re: Using events in a console, non-gui, application ...
« Reply #9 on: February 18, 2025, 04:03:55 pm »
There is room for both implementations @bart. Mine is the quick 'n dirty approach yours the more elegant one  :)

Main takeaway for OP is to see the difference in a normal procedure vs a procedure of object (a so-named method in this case).

Today is tomorrow's yesterday.

xint

  • New Member
  • *
  • Posts: 33
Re: Using events in a console, non-gui, application ...
« Reply #10 on: February 18, 2025, 04:55:24 pm »
On the one hand it's inspiring to see those examples and on the other hand it's so frustrating until it running.
But once the first timer-events fires, it's amazing !

I learned a lot today.

Thank you all very much !
« Last Edit: February 19, 2025, 08:48:06 am by xint »

 

TinyPortal © 2005-2018