Sign in Join

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

Mailing lists / fpc-pascal / 2006-09

Timer for Windows/Linux

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Fabrício F. Kammer

Hi all,

I need to implement a routine on my sofware to be executed each 5 minutes.

I'm using just FPC (without lazarus) becuase my software will run as a console application and because I don't have graphical inteface on the machine that I'm using.

How can I make a routine that works on both OS (Linux/Windows) like a TTimer of the Delphi?

Regards,

Fabrício F. Kammer

Felipe Monteiro de Carvalho
to Fabrício F. Kammer
"Fabrício F. Kammer" wrote
I need to implement a routine on my sofware to be executed each 5 minutes.

Write a loop that first stores the current time, then executed whatever you need, and then sleeps until current_time - previous_time

= 5 minutes. Like this:
    while (not Terminated) do
    begin
      PreviousTime := Now;
      // Do what you need to do
      {----------------------------------------------
        "sleeps" while currentTime - PreviousTime isn´t bigger then 5 minutes
       ----------------------------------------------}
      while (Now - PreviousTime < 5 minutes) do
      begin
        Sleep(1000); // May be bigger
      end;
    end;

You can use the Now function here because 5 minutes a lot of time, and so you don´t really need a lot of precision.

If you needed a nanosecond precision on your loop you could use the powerful EpikTimer component:

http://wiki.lazarus.freepascal.org/EpikTimer

If you want to receive user input while your loop runs, then put the loop on a separate thread.

Felipe Monteiro de Carvalho
Fabrício F. Kammer
to Felipe Monteiro de Carvalho

Thanks Felipe,

Do you have an example of how to use this loop on a thread?

Sorry, but I don't know threads very well.

regards

Fabrício

Felipe Monteiro de Carvalho wrote 35 lines
On 9/1/06, "Fabrício F. Kammer" wrote: > I need to implement a routine on my sofware to be executed each 5 > minutes. Write a loop that first stores the current time, then executed whatever you need, and then sleeps until current_time - previous_time > = 5 minutes. Like this: while (not Terminated) do begin PreviousTime := Now; // Do what you need to do {---------------------------------------------- "sleeps" while currentTime - PreviousTime isn´t bigger then 5 minutes ----------------------------------------------} while (Now - PreviousTime < 5 minutes) do begin Sleep(1000); // May be bigger end; end; You can use the Now function here because 5 minutes a lot of time, and so you don´t really need a lot of precision. If you needed a nanosecond precision on your loop you could use the powerful EpikTimer component: http://wiki.lazarus.freepascal.org/EpikTimer If you want to receive user input while your loop runs, then put the loop on a separate thread.
Fabrício F. Kammer
to Felipe Monteiro de Carvalho

Thank you very much Felipe.

Where are you from?

I'm Brazilian too, from Conchal/SP.

[]s

Fabrício

Felipe Monteiro de Carvalho wrote 14 lines
On 9/1/06, "Fabrício F. Kammer" wrote: > Do you have an example of how to use this loop on a thread? > > Sorry, but I don't know threads very well. Yes, I have a very big example. It´s a GUI for a digital oscilloscope board. The thread code is on the medidor.pas unit You can download it´s source code and full documentation here: http://eletronicalivre.incubadora.fapesp.br/portal/english/oscilloscope/