C#和GDI资源 [英] C# and GDI Resources

查看:90
本文介绍了C#和GDI资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MFC编程中,您必须仔细检查GDI资源的使用情况(例如,

在不再需要时处理它们)。


是否相同适用于C#和.NET?


例如,如果我有


字体f =新字体();

.....用它


f =新字体();


我必须把f.Dispose ()在第二个新版本之前或GC是否为你工作


In MFC programming, you have to carefully check usage of GDI resources (eg
dispose of them when no longer needed).

Does the same apply to C# and .NET ?

eg if I have

Font f = new Font ();

..... use it

f = new Font ();

Do I have to put f.Dispose() in prior to the second new or does GC work it out
for you ?

推荐答案

是的,你应该完成后,在任何一次性物品上调用Dispose()

使用它。对于一次性对象,我的意思是要么实现

IDisposable,要么具有等效语义的方法;比如Close()。它确实是GC将处理这些资源,但是没有

保证何时或甚至是否会发生这种情况。

不要忘记,如果你在同一个词块中创建和处理一个IDisposable对象

,你可以使用C#的using语句来确保

无论是否抛出异常,对象都将被处理掉。这样的

如:


使用(Foo bar = new Foo())

{

bar.DoStuff();

} // bar.Dispose()在这里调用。


Josh


Ian Semmel写道:
Yes, you should call Dispose() on any disposable object once you are finished
using it. By a ''disposable'' object, I mean something which either implements
IDisposable or has a method with equivalent semantics; such as Close(). It
is true that the GC will dispose of such resources, but there are no
guarantees as to when or even if that will happen.

Don''t forget that if you are creating and disposing of an IDisposable object
in the same lexical block, you can use C#''s using statement to ensure that
the object is disposed of regardless of whether an exception is thrown. Such
as:

using( Foo bar = new Foo() )
{
bar.DoStuff();
} // bar.Dispose() is called here.

Josh

"Ian Semmel" wrote:
在MFC编程中,您必须仔细检查GDI资源的使用情况(例如,在不再需要时处理它们)。

这同样适用于C#和.NET吗?

例如,如果我有

字体f =新字体();

....使用它

f = new Font();

我是否必须在第二个新的之前放入f.Dispose()或者GC将其解决? /> for block?
In MFC programming, you have to carefully check usage of GDI resources (eg
dispose of them when no longer needed).

Does the same apply to C# and .NET ?

eg if I have

Font f = new Font ();

..... use it

f = new Font ();

Do I have to put f.Dispose() in prior to the second new or does GC work it out
for you ?



Ian Semmel写道:
Ian Semmel wrote:
在MFC编程中,你必须仔细检查GDI资源的使用情况
(例如,在不再需要时处理它们)。

C#和.NET是否同样适用?

例如,如果我有

字体f =新字体();

....用它

f =新字体();

我有吗把f.Dispose()放在第二个新版本之前还是让GC工作呢?
In MFC programming, you have to carefully check usage of GDI resources
(eg dispose of them when no longer needed).

Does the same apply to C# and .NET ?

eg if I have

Font f = new Font ();

.... use it

f = new Font ();

Do I have to put f.Dispose() in prior to the second new or does GC work
it out for you ?




是的,你应该明确处理任何一个IDIS posable objects(或在using语句中包装

)。


JB



Yes, you should explicitly dispose of any IDisposable objects (or wrap
in a using statement).

JB


在星期二, 2006年6月6日15:58:02 -0700,Josh Smith

< Jo ******* @ discussion.microsoft.com>写道:
On Tue, 6 Jun 2006 15:58:02 -0700, Josh Smith
<Jo*******@discussions.microsoft.com> wrote:
是的,一旦你完成使用它就应该在任何一次性物品上调用Dispose()
Yes, you should call Dispose() on any disposable object once you are finished
using it




我很好奇。如果你不打电话怎么办...会是什么

是常见的症状?


另外,有没有办法设置''标记''当一个可疑程序

运行以便以后可以回收资源?



I''m curious about this. What happens if you don''t call dispose...what
are the usual symptoms?

Also, is there any way to set a ''marker'' when a suspicious program
runs so that resources can be reclaimed afterward?


这篇关于C#和GDI资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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