Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
TForm1 = class(TForm)
private
FLock: TRTLCriticalSection;
InitializeCriticalSection(FLock);
EnterCriticalSection(FLock);
LeaveCriticalSection(FLock);
DeleteCriticalSection(FLock);
EnterCriticalSection(FLock);
try
DoSomethingWithTheProtectedObject;
finally
LeaveCriticalSection(FLock);
end;
type
TObjectClass = class of TObject;
TLocker = class(TObject)
private
FLock: TRTLCriticalSection;
FObject: TObject;
public
constructor Create(AObjectClass: TObjectClass); overload;
constructor Create(AObject: TObject); overload;
destructor Destroy;
function Lock: TObject;
procedure Unlock;
end;
constructor TLocker.Create(AObjectClass: TObjectClass);
begin
InitializeCriticalSection(FLock);
FObject:= AObjectClass.Create;
end;
constructor TLocker.Create(AObject: TObject);
begin
InitializeCriticalSection(FLock);
FObject:= AObject;
end;
destructor TLocker.Destroy;
begin
FObject.Free;
DeleteCriticalSection(FLock);
end;
function TLocker.Lock: TObject;
begin
EnterCriticalSection(FLock);
Result:= FObject; //Note how this is called AFTER the lock is engaged
end;
procedure TLocker.Unlock;
begin
LeaveCriticalSection(FLock);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
O: TMyObject;
begin
O:= TMyObject.Create;
FMyObjectLocker:= TLocker.Create(O);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FMyObjectLocker.Free; //Your original object will be free'd automatically
end;
procedure TForm1.DoSomething;
var
O: TMyObject;
begin
O:= TMyObject(FMyObjectLocker.Lock);
try
O.DoSomething;
//etc...
finally
FMyObjectLocker.Unlock;
end;
end;