I am writing a custom class that should have the ability to connect to a TMemo FireMonkey component on the form in order to log info to it. The class is defined as :
TBlokData = class
private
[weak] FLogMemo: TMemo;
procedure Log(s : string);
public
constructor Create(ConnStr: string);
property LogMemo : TMemo read FLogMemo write FLogMemo;
destructor Destroy; override;
end;
and the implementation of the Log
method is :
procedure TBlokData.Log(s : string);
begin
if Assigned(FLogMemo) then
FLogMemo.Lines.Add(TimeToStr(Now) + ': ' + s);
end;
I am concerned if I create the class object in a thread and populate the LogMemo
property with, say, the Memo1
component on the FireMonkey form, that my class will no longer be thread-safe because I will manipulate a component on the form from a thread when calling the Log
method.
Is this a valid concern? If so, how can I make it thread-safe while maintaining the class' usability outside a threading environment as well?