SITUATION
To better understand the PPL and how a Task
works I tried to make a very easy program in which, once you have clicked a button, a ListBox
is filled with a list of directories in the disk.
procedure TForm3.Button1Click(Sender: TObject);
var proc: ITask;
begin
//Show that something is going to happen
Button1.Caption := 'Process...';
proc := TTask.Create(
procedure
var strPath: string;
sl: TStringDynArray;
begin
if (DirectoryExists('C:\Users\albertoWinVM\Documents\uni\maths')) then
begin
ListBox1.Items.Clear;
sl := TDirectory.GetDirectories('C:\Users\albertoWinVM\Documents\uni\maths',
TSearchOption.soAllDirectories, nil);
for strPath in sl do
begin
ListBox1.Items.Add(strPath);
end;
//At the end of the task, I restore the original caption of the button
Button1.Caption := 'Go';
Label1.Caption := 'Finished';
end;
end
);
proc.Start;
end;
The folder maths
you can see above is not very huge and the task takes about 3 seconds to execute. The task is declared as follows:
type
TForm3 = class(TForm)
ListBox1: TListBox;
//... other published things var ...
private
proc: ITask;
public
//... public var ...
end;
PROBLEM
When I work (for example) with C:\Users\albertoWinVM\Documents
I have a very big amount of folders and the program takes up to 3 minutes before filling the ListBox.
If I closed the program (while the task is still running) having only the code above, from what I have understood reading online, the task will still run until he hasn't finished. Am I correct?
procedure TForm3.FormDestroy(Sender: TObject);
begin
proc.Cancel;
end;
I thought that I could add this code to improve the safety of the program. Is that enough?