Skip to content

Instantly share code, notes, and snippets.

@Sorien
Created September 16, 2020 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sorien/d62c81f853f831f309c3d71bbc80209d to your computer and use it in GitHub Desktop.
Save Sorien/d62c81f853f831f309c3d71bbc80209d to your computer and use it in GitHub Desktop.
unit Unit12;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
OtlSync, OtlTaskControl, OtlTask, OtlParallel;
type
TFoo = class
private
FTask: IOmniTaskControl;
FCancellationToken: IOmniCancellationToken;
public
constructor Create();
destructor Destroy(); override;
procedure RunTask();
end;
TForm12 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form12: TForm12;
implementation
{$R *.dfm}
{ TFoo }
constructor TFoo.Create;
begin
FTask := nil;
FCancellationToken := CreateOmniCancellationToken;
end;
destructor TFoo.Destroy;
begin
FCancellationToken.Signal;
FTask.WaitFor(INFINITE);
inherited;
end;
procedure TFoo.RunTask;
begin
FTask := CreateTask(
procedure(const Task: IOmniTask)
begin
if (not Task.CancellationToken.IsSignalled) and (not Task.Terminated) then
Task.Invoke(
procedure()
begin
Assert(Self <> nil, 'AV');
end);
end).Unobserved;
Parallel.Start(FTask, Parallel.TaskConfig.CancelWith(FCancellationToken));
end;
procedure TForm12.FormCreate(Sender: TObject);
var
F: TFoo;
begin
F := TFoo.Create;
try
F.RunTask;
finally
FreeAndNil(F);
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment