Up until Delphi 10.4.2, there are compiler bugs related to inline variables captured by anonymous methods:
Use a traditional variable declaration instead of an an inline declaration:
procedure Example;
begin
var MyObj := TFoo.Create as IFoo;
TTask.Run(
procedure begin
Sleep(1000);
MyObj.Bar;
end
);
end;
procedure Example;
var
MyObj: IFoo;
begin
MyObj := TFoo.Create;
TTask.Run(
procedure begin
Sleep(1000);
MyObj.Bar;
end
);
end;