unit UThreadStringList;
interface
uses
classes,syncObjs,sysUtils;
type
TThreadStringList =
class
private
FStringList : TStringList;
FCriticalSection : TCriticalSection;
public
constructor create;
destructor destroy;
function lockStringList:TStringList;
procedure unlock;
procedure add(AItem: AnsiString);
function get(AIdx: Integer): AnsiString;
procedure saveToFile(AFilePath: AnsiString);
procedure remove(AIdx: Integer);
function count:Integer;
end;
implementation
constructor TThreadStringList.create;
begin
FStringList := TStringList.Create;
FCriticalSection := TCriticalSection.create;
end;
destructor TThreadStringList.destroy;
begin
freeAndNil(FStringList);
freeAndNil(FCriticalSection);
end;
function TThreadStringList.lockStringList:TStringList;
begin
FCriticalSection.Acquire;
result := FStringList;
end;
procedure TThreadStringList.unlock;
begin
FCriticalSection.Release;
end;
procedure TThreadStringList.add(AItem: AnsiString);
var
sl : TStringList;
begin
sl := lockStringList;
sl.Add(AItem);
unlock;
end;
function TThreadStringList.get(AIdx: Integer): AnsiString;
var
sl : TStringList;
begin
sl := lockStringList;
result := sl[AIdx];
unlock;
end;
procedure TThreadStringList.saveToFile(AFilePath:
string);
var
sl : TStringList;
begin
sl := lockStringList;
try
sl.SaveToFile(AFilePath);
finally
unlock;
end;
end;
procedure TThreadStringList.remove(AIdx: Integer);
var
sl : TStringList;
begin
sl := lockStringList;
sl.Delete(AIdx);
unlock;
end;
function TThreadStringList.count:Integer;
var
sl : TStringList;
begin
sl := lockStringList;
result := sl.count;
unlock;
end;
end.