mardi 14 février 2017

Where can I find TMonitor (Delphi 7) or how can I replace it with an alternative function?

I have a code (Singleton- Pattern) which works with Delphi RAD 10.1

type

  TSharedData = class
  private
    FPOL: integer;
    class var FUniqueInstance: TSharedData;
    procedure SetFPol(const Value: integer);
    constructor Create;
  public
    class function GetInstance: TSharedData;
    property POL: integer read FPOL write SetFPol;
  end;

var
  Key: TObject;

implementation

{ TSharedData }

constructor TSharedData.Create;
begin
  SetFPol(1);
end;

class function TSharedData.GetInstance: TSharedData;
begin
  TMonitor.Enter(Key); // <-- error here
  try
    if FUniqueInstance = nil then
    begin
      FUniqueInstance := TSharedData.Create;
    end;
  finally
    TMonitor.Exit(Key);
  end;
  Result := FUniqueInstance;
end;

procedure TSharedData.SetFPol(const Value: integer);
begin
  FPOL := Value;
end;

initialization
  Key:= TObject.Create;
finalization
  Key.Free;

I need now the same code in Delphi 7. But the compiler said, "TMonitor isn't known".

Where can I find TMonitor or how can I replace it with an alternative function?

I thank you in advance for any information.

Aucun commentaire:

Enregistrer un commentaire