开发者

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 b开发者_运维百科efore 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 (e.g. the program files folder).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜