避免处置系统定义的Pen和Brush实例 [英] Avoiding disposing system-defined Pen and Brush instances

查看:240
本文介绍了避免处置系统定义的Pen和Brush实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解在画笔,但如果已将它们设置为系统预定义的值(例如 System.Drawing.Brushes System.Drawing.Pens System.Drawing.SystemBrushes )

I understand it is best practise to call Dispose() on instances of Pen and Brush, except if they've been set to the system-predefined values (eg. System.Drawing.Brushes, System.Drawing.Pens or System.Drawing.SystemBrushes)

尝试处置系统定义的资源会导致引发异常.

Trying to dispose a system-defined resource results in an exception being thrown.

如何检测(除了将Dispose()调用包装在try/catch中)之外,这些资源之一是引用系统定义的值还是用户定义的值似乎并不明显.

It doesn't appear to be obvious how you can detect (apart from wrapping the Dispose() call in a try/catch) whether one of these resources is referencing a system-defined or user-defined value.

推荐答案

不需要调用Dispose.垃圾收集的目的是消除这些要求.

There is no requirement to call Dispose. The purpose of garbage collection is to eliminate these kinds of requirements.

IDisposable的主要目的之一是允许类在资源受限的环境中清理非托管资源.如果不调用dispose方法,则在垃圾收集过程中对象完成并处理后,将清除该类的非托管资源.

One of the main purposes of IDisposable is to allow a class to clean up unmanaged resources in resource-limited environments. If you do not call the dispose method, the unmanaged resouces of the class will be cleaned up once the object is finialized and disposed during garbage collection.

如果必须"调用dispose并且不知道画笔实例是系统"画笔还是普通"画笔,则必须使用try ... catch块.

If you "must" call dispose and you do not know if the brush instance is a "system-" or a "normal-" brush then you will have to use a try...catch block.

  • 不需要调用SystemBrushesSystemPens上的dispose,因为GDI +库将处理这些资源.
  • 可以处置SystemFontsSystemIcons.
  • It is not needed to call dispose on SystemBrushes and SystemPens because the GDI+ Library will take care of these resources.
  • Okay to dispose of SystemFonts and SystemIcons.

该类的备注部分将记录是否需要调用Dispose方法. 如果备注"部分建议调用Dispose方法,那么我会这样做.

The Remarks section of the class will make note of if there is a requirement to call the Dispose method. If the Remarks section recommends to call the Dispose method, then I will do it.

通常,我不称用笔和刷子处理".如果我有一个图形密集型应用程序或类,那么我将缓存所需的钢笔和画笔的实例.我在应用程序或类的整个生命周期中都使用它们.如果我不这样做,那么尝试创建和处理所有这些对象的次数就会如此频繁,那么图形绘画的性能就会受到影响. (嗯...现在我考虑了一下,性能可能就是为什么我们不能处理SystemBrushes和SystemPens,却可以处理SystemFonts和SystemIcons的原因.甚至该框架也缓存了SystemBrushes和SystemPens.)

In general, I do not call dispose on pens and brushes. If I have a graphic-intensive application or class then I will cache instances of the pens and brushes I need. I use them throughout the life of the application or class. If I didn't do this then graphics painting performance will suffer trying to create and dispose all those ojects so many times and so frequently. (Hm...now that I think about it, performance is probably why we cannot dispose of SystemBrushes and SystemPens, yet can dispose of SystemFonts and SystemIcons. Even the framework caches SystemBrushes and SystemPens.)

这篇关于避免处置系统定义的Pen和Brush实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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