TDirectory.Delete似乎是异步的 [英] TDirectory.Delete seems to be asynchronous

查看:130
本文介绍了TDirectory.Delete似乎是异步的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察到,如果要删除的文件夹中包含的文件很少并且文件夹处于打开状态(在Total Commander中),则在调用TDirectory.Delete(x)后立即调用DirectoryExists(x)会返回true。

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).

换句话说:

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

这是正常现象吗?

解决方案是这样的:

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)?

注意:Total Commander不会阻止删除文件夹(我想),因为无论如何都会删除文件夹(一段时间后)。

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

推荐答案

看看有关RemoveDirectory的msdn页面: https://msdn.microsoft.com/zh-CN/library/windows/desktop/aa365488(v = vs.85).aspx

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

备注部分显示:


RemoveDirectory函数将目录标记为关闭时删除。因此,在关闭目录的最后一个句柄之前,不会删除该目录。

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 ;)

这篇关于TDirectory.Delete似乎是异步的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆