TDirectory.Delete seems to be asynchronous

↘锁芯ラ 提交于 2019-12-07 17:31:49

问题


I have observed that calling DirectoryExists(x) immediately after calling TDirectory.Delete(x) returns true IF the folder to be deleted has few files in it AND the folder is open (in Total Commander).

In other words:

begin 
  TDirectory.Delete('x', true);  <-- 'Delete' exited but the folder is still not fully deleted
  if SysUtils.DirectoryExists('x')...  <-- This returns true
end;

Is this a normal behavior?

The "solution" is this:

begin 
  TDirectory.Delete('x', true); 
  Sleep(1000);          //wait for Delete to finish
  if SysUtils.DirectoryExists('x')...
end;

Question: How do I know when the Delete is ready (how do I eliminate sleep)?

Note: Total Commander does not block the deletion of the folder (I guess) since the folder is deleted anyway (after a while).


回答1:


Take a look the msdn page about RemoveDirectory: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365488(v=vs.85).aspx

The Remarks section says:

The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed.

So probably another process also has a handle to the directory (virus scanner?).

If you need to empty the directory, then empty it instead of deleting it and recreating it afterwards. In the end you always pay for a dirty hack ;)



来源:https://stackoverflow.com/questions/42809389/tdirectory-delete-seems-to-be-asynchronous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!