gpt4 book ai didi

.net - .NET 4.0 文件系统枚举功能的奇怪之处

转载 作者:行者123 更新时间:2023-12-03 15:02:45 25 4
gpt4 key购买 nike

我刚读了一页 "Whats new .NET Framework 4.0" .我无法理解最后一段:

To remove open handles on enumerated directories or files

  1. Create a custom method (or function in Visual Basic) to contain your enumeration code.

  2. Apply the MethodImplAttribute attribute with the NoInlining option to the new method. For example:

    [MethodImplAttribute(MethodImplOptions.NoInlining)] Private void Enumerate()

  3. Include the following method calls, to run after your enumeration code:

      * The GC.Collect() method (no parameters).
    * The GC.WaitForPendingFinalizers() method.


为什么是属性 NoInlining?内联在这里会造成什么危害?

为什么要手动调用垃圾收集器,为什么不首先让枚举器实现 IDisposable 呢?我怀疑他们使用 FindFirstFile()/FindNextFile() API 调用来实现,因此如果枚举完成,无论如何都必须调用 FindClose()。

编辑:

有人知道为什么文章中建议使用 NoInlining 属性吗?

最佳答案

很奇怪。迭代器正确实现了 IDisposable,它调用 FindClose()。 AllDirectories 选项可能是一个麻烦源,因为 FindFileFirst/Next 只允许迭代单个目录。但是我看到迭代器在做正确的事情,它在迭代目录结构时只保持一个句柄打开。

MSDN 文章特别提到“如果枚举的目录或文件之一上仍有打开的句柄”。 FindFileFirst/Next 不会打开句柄。但是在枚举时读取文件的草率用户代码确实如此。 “对文件或目录的删除操作”也很相关,我认为 Vista 中的行为发生了变化。 DeleteFile() 可以成功,但在文件上的所有句柄都关闭之前,该文件实际上不会消失。

我们需要有人自愿而不是在 XP 上实现此代码。我想我们很快就会找到人:)

关于.net - .NET 4.0 文件系统枚举功能的奇怪之处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2663574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com