See steps below how to reproduce. I use Delphi 10.1 Berlin and Windows 10 and compile to win32.
- Create a new VCL Forms Application
- Place a TTimer and a TMemo on the form
- Set the timer's Interval to 10 ms
- Put this code in the OnTimer event:
if FileExists('named.txt') then
begin
Memo1.Lines.Add('named.txt exists');
DeleteFile('renamed.txt'); //delete if it exists
if RenameFile('named.txt', 'renamed.txt') then
Memo1.Lines.Add(' renamed OK')
else
Memo1.Lines.Add(' rename failed with error : '+ IntToStr(GetLastError));
end;
Run the program
Create a file named.txt
TMemo output shows:
named.txt exists renamed OK
- Now rename the file renamed.txt back to named.txt in the explorer.
TMemo output now shows:
named.txt exists renamed OK named.txt exists renamed OK
But there will come an error message showing "File or folder does not exists". Why?
(Renamefile returns OK).
Setting the timer's Interval to e.g 500 ms seems to be ok (no error message).
Here is the message (in Swedish):
I even copied the exe-file to another PC with the same result:


My guess is that one of the lines in the timer event ends up calling Application.ProcessMessages (possibly the adding to the Memo.Lines property). If more than 10ms has occured from the time the timer started executing the event, there will be a new timer event waiting in the message queue, which will trigger a call to the event once again.
In essense, you are then executing statements along these lines:
One solution would be this:
to make sure that no new timer events can occur while you are processing one.