Load Log.txt From other apps to Memo - Delphi7

Pujangga Adhitya picture Pujangga Adhitya · Nov 23, 2012 · Viewed 8.6k times · Source

I am trying to record the session log from other applications (Proxifier) to a Memo. I've tried using the command :

procedure TForm1.TimerTimer(Sender: TObject);
begin
  Memo1.Lines.LoadFromFile('C:\PMASSH\Proxyfier\Profiles\Log.txt');
end;

but at certain times I get an error

enter image description here

Can you help my problem above ? I would really appreciate of all the answers.

Thanks

Answer

David Heffernan picture David Heffernan · Nov 23, 2012

The other program has opened the file with a sharing mode that does not allow other processes to read it. Typically this happens when the other application is writing to the file.

There's not a whole lot you can do about this. This is perfectly normal behaviour, and is to be expected. You can try detecting the error, waiting for a short period of time, and re-trying.

Since you are already running this on a timer, the re-try will just happen. So perhaps you just need to suppress those exceptions:

procedure TForm1.TimerTimer(Sender: TObject); 
begin
  try
    Memo1.Lines.LoadFromFile(...);
  except
    on EFOpenError do
      ; //swallow this error
  end;
end;

Note that detecting EFOpenError is perhaps a little crude. Perhaps there are other failure modes that lead to that error. However, as a first pass, the code above is a decent start.