Icon.FromHandle:我应该释放它,或者致电DestroyIcon? [英] Icon.FromHandle: should I Dispose it, or call DestroyIcon?

查看:508
本文介绍了Icon.FromHandle:我应该释放它,或者致电DestroyIcon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Win32的SHGetFileInfo以得到一个处理属于某个文件的图标。有很多描述如何做到这一点。

I use Win32 SHGetFileInfo to get a handle to the icon belonging to a certain file. There are a lot of descriptions how to do this.

在调用该函数后,你有手柄的图标一个结构。使用静态方法Icon.FromHandle我可以将其转换为类System.Drawing.Icon的对象。这个类实现System.IDisposable的。正确用法是这样的:

After calling the function you have a struct with the handle to the Icon. Using the static method Icon.FromHandle I can convert it to an object of class System.Drawing.Icon. This class implements System.IDisposable. Proper usage would be like:

using (Icon icon = Icon.FromHandle(shFileInfo.hIcon))
{
    // do what you need to do with the icon
}

在离开using语句图标对象设置。

Upon leaving the using statement the icon object is disposed.

MSDN警告中的 Icon.FromHandle(钟查看)

在使用此方法,您必须使用DestroyIcon方法在Win32 API中,以确保资源被释放丢弃原图标。

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.

而在图标.Dispose(点击查看)

释放由该图标的所有资源。

Releases all resources used by this Icon.

问:

是不是足够的Dispose()的对象,或者我应该叫双方的Dispose()和DestroyIcon,或者致电处置的对象DestroyIcon呢?

推荐答案

在.NET Icon类是非常笨拙的,趁着这个自己需要照顾。对于SHFILEICON MSDN文章不讳言的话,你的必须的调用DestroyIcon()。所以做了Icon.FromHandle MSDN文章()。在时间的确切时刻调用DestroyIcon重要的一个很大的为好,它必须被推迟到的部分的code要么取得了图标的副本,或者直到你不再需要的图标,并会通常调用它的Dispose()方法。

The .NET Icon class is remarkably clumsy, taking care of this yourself is required. The MSDN article for SHFILEICON makes no bones about it, you must call DestroyIcon(). And so does the MSDN article for Icon.FromHandle(). The exact moment in time you call DestroyIcon matters a great deal as well, it must be delayed until some code either has made a copy of the icon or until you no longer need the icon and would normally call its Dispose() method.

当心MSDN文章在code段的,它显示了这样一个场景:DestroyIcon()被称为早期。没关系在于特定的情况下,由于它被分配给Form.Icon属性。一个角落的情况下,肯定不是你想要做什么。

Beware of the code snippet in the MSDN article, it shows a scenario where DestroyIcon() is called early. Okay in that specific case since it is assigned to the Form.Icon property. A corner case and surely not what you want to do.

唯一真正体面的方式来处理,这是覆盖Icon.FromHandle(的行为),并强制对象采取原生图标句柄的所有权。因此,它会自动调用DestroyIcon(),当你处理它。这需要一个黑客,图标构造函数,可以让你做到这一点的的内部的。反射来救援,就可以使用code 从这个帖子,注意GetConstructor()调用。通过编写一个小单元测试,这是否一百万次开始感觉良好。如果你恨它,然后写自己的IDisposable的包装,所以你既可以处理的图标的的PInvoke的DestroyIcon()。

The only really decent way to deal with this is to override the behavior of Icon.FromHandle() and force the object to take ownership of the native icon handle. So that it will automatically call DestroyIcon() when you dispose it. That requires a hack, the Icon constructor that lets you do this is internal. Reflection to the rescue, you can use the code from this post, note the GetConstructor() call. Start feeling good about it by writing a little unit test that does this a million times. If you hate it then write your own IDisposable wrapper so you can both dispose the icon and pinvoke DestroyIcon().

这篇关于Icon.FromHandle:我应该释放它,或者致电DestroyIcon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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