TThread.Queue
of the newer versions?
What I need is to implement a queue for logging with Delphi 6. In newer versions I have the ability to use:
procedure TFormClient.QueueLogMsg(const s: string);
begin
TThread.Queue(nil,
procedure
begin
LogMsg(s)
end
);
end;
I've been thinking about creating a thread that would do this, but I do not know if I'll get errors when trying to add a new process to the process list while the thread is running.
What I thought:
TQueueLog = class(TThread)
private
FLog: TStringList;
FFile: TStringList;
FFileName: string;
public
constructor Create; reintroduce;
destructor Destroy; override;
procedure AddLog(value: string);
procedure Execute; override;
end;
...
constructor TQueueLog.Create;
begin
inherited Create(false);
FreeOnTerminate := false;
FLog := TStringList.Create;
FFile := TStringList.Create;
FFileName := 'log.txt';
end;
destructor TQueueLog.Destroy;
begin
FLog.Free;
FFile.Free;
inherited;
end;
And the methods AddLog
and Execute
:
procedure TQueueLog.AddLog(value: string);
begin
FLog.Add(value);
end;
procedure TQueueLog.Execute;
var
count: integer;
begin
while (not Self.Terminated) do
begin
if (FLog.Count > 0) then
begin
FFile.LoadFromFile(FFileName);
for count := 0 to pred(FLog.Count) do
begin
FFile.Add(FLog.Strings[count]);
end;
FFile.SaveToFile(FFileName);
FLog.Clear;
end;
end;
end;
Then, How to prevent access violation when trying to add an item to FLog
by
AddLog
when the thread is running? Is there another correct way to implement this?