方法在 3310 次成功后失败

Method failing after 3310 successes

我继承了以下扩展方法,它根据文件路径创建一个 ImageSource 对象

public static class ImageSourceExtensions
{
    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
        public IntPtr hIcon;
        public int iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };

    public const uint SHGFI_ICON = 0x100;
    public const uint SHGFI_LARGEICON = 0x0;

    [DllImport("shell32.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);

    public static ImageSource GetIconFromFolder(this string filePath)
    {
        SHFILEINFO shinfo = new SHFILEINFO();
        SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
                SHGFI_ICON | SHGFI_LARGEICON);

        using (Icon i = Icon.FromHandle(shinfo.hIcon))
        {
            //Convert icon to a Bitmap source
            ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
                                    i.Handle,
                                    new Int32Rect(0, 0, i.Width, i.Height),
                                    BitmapSizeOptions.FromEmptyOptions());

            return img;
        }
    }
}

此扩展方法对文件夹和文件完美运行 3310 次。在第 3311 次方法调用时,抛出以下异常:

'Win32 handle that was passed to Icon is not valid or is the wrong type'

使用 shinfo.hIcon 属性 时会抛出此错误。

错误最初是在我的主应用程序中引发的,并且每次都在不同的文件中。我已经将这个 class 提取到一个新项目中,并且可以通过单击按钮执行此操作来抛出相同的错误(在相同的次数之后):

String path = @"C:\Generic Folder\Generic Document.pdf";

        for (int i = 0; i <4000; i++)
        {
            ImageSource img = path.GetIconFromFolder();
        }

有人知道造成这种情况的明显原因吗?

您 运行 已满 HANDLE 秒。 Windows 的句柄数量有限,当您获得一个句柄(在本例中为图标句柄)时,您应该在不再使用它后释放它。如果你不这样做,Windows 将 运行 出可用的句柄来扔掉。

If SHGetFileInfo returns an icon handle in the hIcon member of the SHFILEINFO structure pointed to by psfi, you are responsible for freeing it with DestroyIcon when you no longer need it.

因此您需要 PInvoke DestroyIcon 并在函数结束时将 shinfo.hIcon 传递给它。

FromHandle创建的Icon的处理不会破坏原来的句柄:

When using this method, you must dispose of the original icon by using the DestroyIcon method in the Win32 API to ensure that the resources are released.