Bu "threadsafe" genel özelliğini oluşturdum, ana iş parçacığı ile arka plan iş parçacığı arasında kullanabileceğim. Bunu yaptım çünkü tüm özelliklerim ve değişkenlerim için Lock-Objects yaratmaktan bıktım.Genel Threadsafe Özelliği
TLockedProp<MyType> = class
private
FMyProp:MyType;
PropLock:TObject;
procedure SetMyProp(const Value: MyType);
function GetMyProp: MyType;
published
property Value:MyType read GetMyProp write SetMyProp;
public
Constructor Create;
Destructor Destroy;override;
end;
{ TLockedProp<MyType> }
constructor TLockedProp<MyType>.Create;
begin
inherited;
PropLock:=TObject.create
end;
destructor TLockedProp<MyType>.Destroy;
begin
PropLock.Free;
inherited;
end;
function TLockedProp<MyType>.GetMyProp: MyType;
begin
TMonitor.Enter(PropLock);
result := FMyProp;
TMonitor.Exit(PropLock);
end;
procedure TLockedProp<MyType>.SetMyProp(const Value: MyType);
begin
TMonitor.Enter(PropLock);
FMyProp := Value;
TMonitor.Exit(PropLock);
end;
Karşılaştığım herhangi bir sorun var mı? Bu özellik sınıfını kullanan bazı kodlardır. Bana ne düşündüğünü söyle.
TBgThread=class(TThread)
private
FPaused: TLockedProp<boolean>;
FCount:TLockedProp<integer>;
procedure ChangeCount(pPlusMin:integer);
function GetPaused:boolean;
function GetCount:integer;
public
constructor Create;
destructor Destroy;override;
{Toggle Pause}
procedure PausePlay;
protected
procedure Execute;override;
published
Property Paused:boolean read GetPaused;
Property Count:integer read GetCount;
end;
constructor TBgThread.Create();
begin
inherited Create(true);;
FPaused:=TLockedProp<boolean>.create;
FPaused.Value:=false;
FCount:=TLockedProp<integer>.create;
FCount.Value:=0;
end;
destructor TBgThread.Destroy;
begin
FPaused.Free;
FCount.free;
inherited;
end;
procedure TBgThread.Execute;
begin
inherited;
Repeat
if not Paused then begin
Try
//do something
finally
ChangeCount(+1);
end;
end else
Sleep(90);
Until Terminated;
end;
function TBgThread.GetCount: integer;
begin
Result:=FCount.Value;
end;
procedure TBgThread.ChangeCount(pPlusMin: integer);
begin
FCount.Value:=FCount.Value+pPlusMin;
end;
function TBgThread.GetPaused: boolean;
begin
result := FPaused.Value;
end;
procedure TBgThread.PausePlay;
begin
FPaused.Value:=not FPaused.Value;
end;
Cevabınız için Thnx, ve sınıfınızın sürümünü paylaştığınız için gerçekten minnettarım. Bu çözümle kendim gelebildiğim bir programcı olarak kendimi daha güvende hissetmemi sağlıyor. kemerimin altında sadece 1 yıl delphi;). –
Önemli bir bölümün arabellek tutan sınıfı CPU önbellek satırından küçükse performans sorunlarının olabileceğini unutmayın. Bkz. Https://www.delphitools.info/2011/11/30/fixing-tcriticalsection/ Sınıf tanımına küçük bir hizalama tamponu eklemek ve TCriticalSection sınıfı yerine OS kritik bölümüne güvenmek daha iyi/güvenli olabilir. . –