Skip to content

Instantly share code, notes, and snippets.

@abdiiwan
Created December 2, 2016 19:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdiiwan/1ce36cd8e2cd5fd472e19c7c161cfe3f to your computer and use it in GitHub Desktop.
Save abdiiwan/1ce36cd8e2cd5fd472e19c7c161cfe3f to your computer and use it in GitHub Desktop.
In Delphi to "async/await". Use "TTask"
procedure TForm1.Button1Click(Sender: TObject);
var
http: TIdHTTP;
begin
FstTest := TTask.Future<String>(function():String begin
Sleep(3000);
http := TIdHTTP.Create();
try
Result := http.Get('http://www.mojeld.com/');
finally
http.DisposeOf;
end;
end);
//Memo1.Lines.Text := FstTest.Value;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Lines.Text := FstTest.Value;
end;
@reshith
Copy link

reshith commented Aug 10, 2023

I try:
procedure TForm1.Button1Click(Sender: TObject);
var
LFuture: IFuture;
begin
mmoLog.Lines.Add(Format('Start!', []));

LFuture := TTask.Future(
function(): String
begin
Sleep(3000);

  Result := 'I''m Future.';
end
);

mmoLog.Lines.Add(LFuture.Value);

mmoLog.Lines.Add(Format('End!', []));
end;

Output:
Start!
I'm Future.
End!

Output is OK.
But UI is blocked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment