Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric Threadsafe Property
    primarykey
    data
    text
    <p>I have created this "threadsafe" generic property, that i can use between the main thread and a background Thread. I made it because i was tired of creating Lock-Objects for all my properties and variables.</p> <pre><code>TLockedProp&lt;MyType&gt; = 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&lt;MyType&gt; } constructor TLockedProp&lt;MyType&gt;.Create; begin inherited; PropLock:=TObject.create end; destructor TLockedProp&lt;MyType&gt;.Destroy; begin PropLock.Free; inherited; end; function TLockedProp&lt;MyType&gt;.GetMyProp: MyType; begin TMonitor.Enter(PropLock); result := FMyProp; TMonitor.Exit(PropLock); end; procedure TLockedProp&lt;MyType&gt;.SetMyProp(const Value: MyType); begin TMonitor.Enter(PropLock); FMyProp := Value; TMonitor.Exit(PropLock); end; </code></pre> <p>Are there any problems i am overlooking? This is some code using this property class. Tell me what you think.</p> <pre><code>TBgThread=class(TThread) private FPaused: TLockedProp&lt;boolean&gt;; FCount:TLockedProp&lt;integer&gt;; 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&lt;boolean&gt;.create; FPaused.Value:=false; FCount:=TLockedProp&lt;integer&gt;.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; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload