我正在编写一个写入事件日志的线程.当应用程序关闭(正常)时,我需要确保此线程完成其作业,在它被释放之前保存日志.如果我Free直接调用该线程,它不应该立即被销毁,它应该等到线程完成并且还有其他工作要做.
这是我如何布置我的线程的执行:
procedure TEventLogger.Execute;
var
L: TList;
E: PEventLog; //Custom record pointer
begin
while not Terminated do begin //Repeat continuously until terminated
try
E:= nil;
L:= LockList; //Acquire locked queue of logs to be written
try
if L.Count > 0 then begin //Check if any logs exist in queue
E:= PEventLog(L[0]); //Get next log from queue
L.Delete(0); //Remove log from queue
end;
finally
UnlockList;
end;
if E <> nil then begin
WriteEventLog(E); //Actual call to save log
end; …Run Code Online (Sandbox Code Playgroud)