unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls,
System.Threading;
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
fTask: ITask;
procedure DoCancelTask;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.FormDestroy(Sender: TObject);
begin
DoCancelTask;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MainTaskProc: TProc;
begin
Button1.Enabled := false;
Button1.Text := '
Task running';
MainTaskProc :=
procedure()
var
UpdateDateTime: TThreadProcedure;
ReactivateButton: TThreadProcedure;
begin
UpdateDateTime :=
procedure()
begin
if Assigned(Button1)
then
begin
Button1.Text := DateTimeToStr(Now);
end;
end;
ReactivateButton :=
procedure()
begin
if Assigned(Button1)
then
begin
Button1.Enabled := true;
Button1.Text := '
Start Task';
end;
end;
repeat
if TTask.CurrentTask.Status = TTaskStatus.Canceled
then
begin
Break;
end;
Sleep(10);
TThread.Synchronize(
nil, UpdateDateTime);
until false;
Sleep(100);
TThread.Synchronize(
nil, ReactivateButton);
end;
fTask := TTask.Run(MainTaskProc);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
DoCancelTask;
end;
procedure TForm1.DoCancelTask;
begin
if Assigned(fTask)
then
begin
fTask.Cancel;
end;
end;
end.