如果在font变量中使用,如何处理字体对象 [英] How does the font object get disposed if used in font variable

查看:82
本文介绍了如果在font变量中使用,如何处理字体对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专业人士,



我在c#winforms中有这样的代码:



{

字体 myFont =新字体(Arial,12);

....一些代码 myFont



myfont =新字体(Times New Roman,8);

....其他代码 myfont









}



在这种情况下, myFont 会在一次大括号内被处理掉,或者不是

或我们应该单独处理?请指导我。



提前致谢。



我尝试过:



尝试搜索谷歌并阅读有关垃圾收集的文档,但我没有清楚了解我想要的内容。

解决方案

我个人会遵循MSDN的建议:字体.Dispose方法(System.Drawing) [ ^ ]



请参阅备注,注意:

< blockquote class =quote>

Quote:

在释放对Font的最后一个引用之前,始终调用Dispose。否则,在垃圾收集器调用Font对象的Finalize方法之前,它将使用的资源不会被释放。


我想根据假设建议一种不同的方法您没有创建数百个不同的字体实例。



a。在Application wide或Form wide范围内创建您将要使用的字体实例。



b。使用它们



c。当应用程序关闭时,字体将被垃圾收集。



d。如果你对字体处理真的很偏执:

 使用(字体arial12 =  new 字体(  Arial 12  ))
{
// 使用字体
}

我从未见过需要明确处理字体的WinForms应用程序。



有关字体的更多详细信息,请参阅StackOverFlow上的此讨论实际上,围绕'Dispose with Fonts:[ ^ ]。您可能也对此处的讨论感兴趣[ ^ ]。



请注意,您可能无法控制某些WinForm控件在内部管理字体的方式(RichTextBox,TreeView,例如)。


Dear Professionals,

I have a code like this in c# winforms:

{
Font myFont = new Font("Arial",12);
.... some code with myFont

myfont = new Font("Times New Roman", 8);
.... some other code with myfont

.
.
.
}

In this case will myFont get disposed as one once out of the braces or not
or should we dispose individually ? Please guide me on this.

Thanks in advance.

What I have tried:

Tried searching on google and read documentation on garbage collection but I did not get a clear light on what I want.

解决方案

I personally would follow MSDN's suggestion: Font.Dispose Method (System.Drawing)[^]

See there Remarks, Note:

Quote:

Always call Dispose before you release your last reference to the Font. Otherwise, the resources it is using will not be freed until the garbage collector calls the Font object's Finalize method.


I'd like to suggest a different approach based on the assumption that you are not creating hundreds of different instances of Fonts.

a. in Application wide, or Form wide, scope create instances of the Fonts you are going to use.

b. use them

c. when the Application closes, the Fonts are going to be garbage collected.

d. if you are really paranoid about Font disposal:

using (Font arial12 = new Font("Arial",12))
{
      // use the Font
}

I have never seen a WinForms app yet that needed to explicitly dispose of Fonts.

See this discussion on StackOverFlow for further details on what a Font actually is, and issues around 'Dispose with Fonts: [^]. You might also be interested in the discussion here [^].

Note that you may have no ability to control the way some of your WinForm Controls internally manage Fonts (RichTextBox, TreeView, for example).


这篇关于如果在font变量中使用,如何处理字体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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