Browser width is too narrow.

Rotate the screen or increase the width.
▲

Example in Object Pascal using coroutines

This is the simplest example in Object Pascal using COMTAY. The example applies to both Delphi and Free Pascal.

The program just prints the "Hello, world!" text three times.

The TCotask is the base class declared and described in the Comtay unit. This class contains the Body dummy method-coroutine.

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.Start;

  Cotask1.Free;
  Cotask2.Free;

  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.
Hello,world! using COMTAY