Answer the question
In order to leave comments, you need to log in
Delphi Async Callable?
Colleagues, welcome.
The question, a call of asynchronous functions in Delphi interests.
At the first stage, I created the following unit
unit TD.AsyncFunc;
interface
uses
System.classes;
type
TAsyncCallProc = reference to procedure;
type
TAsyncFuncThread = class(TThread)
private
Fproc: TAsyncCallProc;
{ private declarations }
protected
procedure Execute; override;
public
constructor Create(const proc: TAsyncCallProc);
published
{ published declarations }
end;
procedure AsyncCallProc(const proc: TAsyncCallProc);
implementation
procedure AsyncCallProc(const proc: TAsyncCallProc);
var
th: TAsyncFuncThread;
begin
th:= TAsyncFuncThread.Create(proc)
end;
{ TAsyncFuncThread }
constructor TAsyncFuncThread.Create(const proc: TAsyncCallProc);
begin
Fproc := proc;
FreeOnTerminate := true;
inherited Create(false);
end;
procedure TAsyncFuncThread.Execute;
begin
Fproc;
end;
end.
procedure TMainForm.Button3Click(Sender: TObject);
begin
AsyncCallProc(
procedure
begin
(Sender as TButton).Text := 'Начал выполнение';
Sleep(5000);
(Sender as TButton).Text := 'Закончил выполнение';
end);
end;
Answer the question
In order to leave comments, you need to log in
And what, exactly, is the question? To make the code thread-safe, the call to the GUI must be wrapped in the Synchronize method (the call to Sleep, of course, does not need to be wrapped).
Here is a tutorial on using TTask and synchronization: Using Tasks from the Parallel Programming Library
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question