Timer to rotate certain time

-1

I need to make a timer that runs every day at 6 pm and press a Button. I did not find anything on the internet about this, any suggestions?

I tried the following, but it does nothing:

  

// INTERVAL TIMER: 60000

var
  hora: TDateTime;
begin
hora := now;
  if hora = strtotime('18:00:00') then
  begin
    Button2.Click;
  end;
end;
    
asked by anonymous 13.09.2014 / 03:48

1 answer

2

For this you need something that makes the verification from time to time. A single routine that does not repeat itself will not work.

One simple way to do this is to use the Delphi Timer component. It is located in the System tab of the component palette.

Here:

Orhere,dependingontheversionofdelphiandthelayoutyouareusing:

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

If you need precision at the time of execution, then your interval (property Interval ) should equal 1000.

Once this is done, just double-click the Timer component to create your main method, which is the OnTimer .

So you need to write the routine for this method:

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

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

If you do not need precision at the time of execution and only want to have a task running close to that time, then you can save on Timer check cycles as follows:

Since you want to report an inverval equal to 60,000 (60 thousand milliseconds = 60 min = 1 hour) you can do this:

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

private
  FUltimaExecucao: TDate;
end;

It can be started with zero.

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

And then check it 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;

So, you will not have precision of the time that was run, but you will have fewer runs of the Timer component.

    
13.09.2014 / 15:14