简体   繁体   中英

Delphi Tstringlist, write file synch

I make a very simple log with tstringlist. I write it to file:

pLog.SaveToFile(FileName);

Somewhere there is a bug, and my computer is shut-down. After, I cannot find my log file. Probably the file is saved in asych mode. There is a way for waiting for the write before to go on with the execution?

Thanks, Alberto

If the savetofile call is at the program end, and the program terminates abnormally, then this call probably won't execute. I use a logging mechanism which for every log call opens the log file, writes the log text and time, flushes and then closes the log file. This way, the log text is guaranteed to be written to a file, even if the program terminates abnormally.

Here's the code:

procedure TMainForm.Log (const s: string);
var
 f: textfile;

begin
 assignfile (f, logfilename);
 {$I-}  // yes, I know: modern programming style requires a try/except block
 append (f);
 if ioresult <> 0 then rewrite (f);
 {$I+}
 writeln (f, datetostr (now), ' ', timetostr (now), ' ', s);
 flush (f);
 closefile (f);
end;

TStringList.SaveToFile operates synchronously.

The most obvious causes of the problem are:

  1. You are not calling TStringList.SaveToFile .
  2. The file name is invalid (perhaps the path doesn't exist).
  3. The file name is in a folder to which your user doesn't have write access (eg the program files folder).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM