This is the simplest example in Object Pascal using COMTAY.
The program prints the text "Hello, world!" three times.
TCotask is the base class declared and described in the Comtay unit.
This class contains the Body method-coroutine (dummy).
Derived classes TCotask1 and TCotask2 have overridden Body methods with behavior description corresponding to each class.
They interact with each other using the Resume and Suspend methods.
program COMTAYexample;
{$S-}
uses
Comtay;
type
TCotask1 = class(TCoTask)
protected
procedure Body; override;
end;
TCotask2 = class(TCoTask)
protected
procedure Body; override;
end;
var
Cotask1: TCotask1;
Cotask2: TCotask2;
procedure TCotask1.Body;
var
i: Integer;
begin
for i:=1 to 3 do
begin
Write('Hello, ');
Resume(Cotask2);
end;
end;
procedure TCotask2.Body;
begin
repeat
WriteLn('World!');
Suspend;
until false;
end;
begin
InitializeComtay;
Cotask1:=TCotask1.Create;
Cotask2:=TCotask2.Create;
Cotask1.ResumeAsMain;
Cotask1.Free;
Cotask2.Free;
FinalizeComtay;
Write(EOL,'Press ENTER for program termination');
ReadLn;
end.
The COMTAY package contains several example projects for Delphi and Lazarus to help you get started with coroutines.