In what way is this thread safe? -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Maurice Butler Sent: Thursday, 17 May 2007 10:48 p.m. To: 'NZ Borland Developers Group - Delphi List' Subject: RE: [DUG] Best way to make this thread safe
May be of use - the trick is at the bottom - Initialization & Finalization Maurice unit ErrorLog; interface function Logfile(comment:string):boolean; implementation uses sysutils, ascii, syncobjs {required for critical section}, Forms {required for access to application title}; var ExclusiveUse : tCriticalSection; TempLog :TextFile; sFileName : string; Procedure OpenLog; Begin sFileName := Application.ExeName + '.' + formatdatetime('yyyymmdd',now)+ '.log'; AssignFile(TempLog, sFileName); //open file if it exists else create if FileExists(sFileName) then //adding to existing file Append(TempLog) else //no file exist so create begin rewrite(Templog); Writeln(TempLog, 'Created for ',Application.ExeName, ' on ', datetostr(date),' at ',timetostr(time)); writeln(TempLog, '=========================================================================== ==='); end; End; Function Logfile(comment:string):boolean; begin ExclusiveUse.Enter; try result := FALSE; OpenLog; try //if string contains second line move the start clear of the date if Pos(Ascii_LF, comment) <> 0 then insert(StringOfChar(' ', 20), Comment, Pos(Ascii_LF, comment)+1); writeln(TempLog, datetostr(date),' ',timetostr(time),', ', comment); result := TRUE; finally //so no loose ends are left closeFile(TempLog); end; finally ExclusiveUse.Leave; end; end; initialization begin ExclusiveUse := TCriticalSection.Create; end; Finalization begin ExclusiveUse.Free; end; end. _______________________________________________ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to [EMAIL PROTECTED] with Subject: unsubscribe _______________________________________________ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to [EMAIL PROTECTED] with Subject: unsubscribe