Created
December 2, 2016 19:35
-
-
Save abdiiwan/1ce36cd8e2cd5fd472e19c7c161cfe3f to your computer and use it in GitHub Desktop.
In Delphi to "async/await". Use "TTask"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I try:
procedure TForm1.Button1Click(Sender: TObject);
var
LFuture: IFuture;
begin
mmoLog.Lines.Add(Format('Start!', []));
LFuture := TTask.Future(
function(): String
begin
Sleep(3000);
mmoLog.Lines.Add(LFuture.Value);
mmoLog.Lines.Add(Format('End!', []));
end;
Output:
Start!
I'm Future.
End!
Output is OK.
But UI is blocked.