I need to perform a task every x seconds and I can accomplish that goal using the code below:
var task: ITask;
begin
task := TTask.Create(
procedure()
begin
//infinite loop
while 1 = 1 do
begin
//time interval
sleep(15000);
//do something every x seconds
end;
end);
task.Start;
However, when using sleep (x) I cannot finish the task at any time, for example, when I close the form (I'm using VCL).
What would be an alternative to perform a task infinitely at a fixed interval, but with the possibility of interrupting the task without waiting for sleep to finish? I saw an article about using TEvent, however I didn't quite understand how it works.