-
-
Save Sorien/d62c81f853f831f309c3d71bbc80209d to your computer and use it in GitHub Desktop.
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
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