4
How to implement a process queue in Delphi 6 as the TThread.Queue
of the newer versions?
What I need is to implement a queue for recording logs with Delphi 6. In the newer versions I have the possibility 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 don’t 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;
So, how to prevent access violation when trying to add an item to FLog
for
AddLog
when the thread is running? There is another, correct, way to implement this?