Yes, you can use that approach, but it is not flexible because your processing function depends on the specific UI type.
I wouldn't create task inside Execute method because processing functionality itself should be implemented as clean synchronous code - that gives you more flexibility to combine its functionality as needed.
I would use something like:
TMyClass = class
public
procedure Execute(OnCompleted: TProc);
end;
procedure TMyClass.Execute(OnCompleted: TProc);
begin
// process data
//...
if Assigned(OnCompleted) then
OnCompleted;
end;
procedure TMyForm.ProcessItem(li: TListViewItem);
begin
TTask.Run(
procedure
begin
TMyClass(li.Data).Execute(
procedure
begin
TThread.Queue(nil,
procedure
begin
UpdateItem(li);
end);
end);
end);
end;
procedure TMyForm.UpdateItem(li: TListViewItem);
begin
li. ...
end;
procedure TMyForm.ProcessView;
begin
for var li in lview1.Items do
begin
ProcessItem(li);
end;
end;