[fpc-pascal] Timer for Windows/Linux

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Fri Sep 1 16:39:27 CEST 2006


On 9/1/06, "Fabrício F. Kammer" <ffkammer at conchalnet.com.br> 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



More information about the fpc-pascal mailing list