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.