Timer run given hour

Asked

Viewed 5,645 times

-1

I need to make a timer that runs every day at 18 hours and press a Button. I couldn’t find anything on the internet about this, any suggestions ?

I tried the next, but he does nothing:

// TIMER INTERVAL: 60000

var
  hora: TDateTime;
begin
hora := now;
  if hora = strtotime('18:00:00') then
  begin
    Button2.Click;
  end;
end;
  • @Tiago, that’s right, will be a FORM with a TIMER that every day at 18:00hrs he should just click on a button Button2.click, that’s all it is.

1 answer

3


For this you need something to check from time to time.
A unique routine that does not repeat itself will not serve.

A simple way to do this is to use the component Timer of Delphi.
It is in the tab System of the component pallet.

Here:

TTimer - classic layout

Or here, depending on the version of Delphi and the layout you are using:

TTImer - classic layout

So just add this component to your Form and make sure the property Enable is True.

If you need accuracy at the time of execution, then your interval (property Interval) shall be equal to 1000.

Once done, just double-click the component Timer to create its main method, which is of the event Ontimer.

So you need to write the routine of this method:

procedure TMeuForm.timerTarefaTimer(Sender: TObject);
begin
  if Time = StrToTime('18:00:00') then
    btnTarefa.Click;
    // ou btnTarefaClick(nil);
end;

Thus, with 1000 milliseconds set in the Inverval of the component Timer, you will have the process running at exactly 18hrs as reported in the code.


If you don’t need accuracy at runtime and just want to have a task run close to that time, then you can save on check cycles from Timer as follows:

Since you intend to report an inverting equal to 60000 (60 thousand milliseconds = 60 min. = 1 hour) you can do so:

Add to your Form a variable that will store the date and time of the last day the task was executed.

private
  FUltimaExecucao: TDate;
end;

It can be started with zero.

procedure TMeuForm.FormCreate(Sender: TObject);
begin
  FUltimaExecucao := 0;
end;

And then perform your verification as follows:

procedure TMeuForm.timerTarefaTimer(Sender: TObject);
begin
  if (Time >= StrToTime('18:00:00')) and (Time <= StrToTime('19:00:00')) and 
    (FUltimaExecucao < Date)then
  begin
    btnTarefa.Click;
    // ou btnTarefaClick(nil);
    FUltimaExecucao = Date;
  end;
end;

Thus, you will not have accuracy of the time that was executed, but will have fewer cycles of component execution Timer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.